XAML 如何在WinUI3中对事件设置组合框控件的样式

whitzsjs  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(131)

我试图在WinUI 3中设计一个组合框的样式。当我将鼠标悬停在组合框上时,背景变为透明,这很糟糕,因为组合框文本是白色的,窗口背景是白色的,这意味着文本不可见。
我想更改ComboBoxBackgroundPointerOver或前景,以便文本不会消失在窗口背景中。
有人知道一个好方法来做到这一点吗?
款式:

<Style x:Key="ComboBoxStyle" TargetType="ComboBox">
        <Setter Property="Foreground" Value="#FFFFFF" />
        <Setter Property="Background" Value="#BF311A" />
        <Setter Property="BorderBrush" Value="#58585A" />
        <Setter Property="FontWeight" Value="Normal"/>
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="MaxDropDownHeight" Value="200"/>
        <Setter Property="MinWidth" Value="200"/>
        <Setter Property="Padding" Value="10"/>
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="ComboBoxItem">
                    <Setter Property="Background" Value="#7E1416"/>
                    <Setter Property="Foreground" Value="#FFFFFF"/>
                    <Setter Property="BorderThickness" Value="0"/>
                    <Setter Property="VerticalAlignment" Value="Bottom"/>
                    <Setter Property="Padding" Value="10"/>
                </Style>
            </Setter.Value>
        </Setter>
    </Style>

我已经看过了,似乎唯一的方法就是使用ControlTemplates,这对我来说似乎有点大材小用,因为你基本上是在重新设计控件。
我也试过VisualStateManager和改变Combobox.Resource的值,我没有得到工作,这是一个糟糕的选择,因为我想颜色设置的风格。
在WinUI 3中似乎不支持触发器。
我很惊讶这不起作用,但解决方案仍然不是理想的,因为我希望这一切都由风格完成

<ComboBox
            ItemsSource="{Binding Runs}"
            Style="{StaticResource ComboBoxStyle}">
        <ComboBox.Resources>
            <Color x:Key="BlackColour">#000000</Color>
            <Color x:Key="WhiteColour">#FFFFFF</Color>
            <StaticResource x:Key="ComboBoxBackgroundPointerOver" ResourceKey="BlackColour"/>
            <StaticResource x:Key="ComboBoxBackground" ResourceKey="BlackColour"/>
        </ComboBox.Resources>
    </ComboBox>
mum43rcc

mum43rcc1#

尝试基于DefaultComboBoxStyle创建Style,这样就可以覆盖像ComboBoxForegroundPointerOver这样的静态资源。

<SolidColorBrush
    x:Key="ComboBoxForegroundPointerOver"
    Color="Black" />
<Style
    x:Key="ComboBoxStyle"
    BasedOn="{StaticResource DefaultComboBoxStyle}"
    TargetType="ComboBox">
    ...
    ...
</Style>

相关问题