【问题标题】:Why is WPF ComboBox styling not as expected?为什么 WPF ComboBox 样式不符合预期?
【发布时间】:2012-10-17 22:09:03
【问题描述】:

我有一个带有 ComboBox 的 WPF 应用程序,我想在其中设置项目的样式,但会根据选择的项目出现意外行为。

以下 XAML sn-p 演示了该问题:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>
    <Style x:Key="ItemStyle" TargetType="{x:Type ComboBoxItem}">
        <Style.Triggers>
            <Trigger Property="Content" Value="ABC">
                <Setter Property="FontStyle" Value="Oblique"/>
                <Setter Property="Foreground" Value="Red"/>
            </Trigger>
        </Style.Triggers>
    </Style>
    <Style x:Key="BoxStyle" TargetType="{x:Type ComboBox}">
        <Style.Triggers>
            <Trigger Property="Text" Value="ABC">
                <Setter Property="FontStyle" Value="Italic"/>
                <Setter Property="Foreground" Value="Red"/>
            </Trigger>
        </Style.Triggers>
    </Style>
  </Page.Resources>
  <Border Width="200">  
      <ComboBox Style="{StaticResource BoxStyle}"
                ItemContainerStyle="{StaticResource ItemStyle}"
                Height="20">
          <ComboBoxItem>ABC</ComboBoxItem>
          <ComboBoxItem>DEF</ComboBoxItem>
          <ComboBoxItem>GHI</ComboBoxItem>
      </ComboBox>
  </Border>
</Page>

这显示了一个简单的 ComboBox,其中包含三个项目; ABC、DEF 和 GHI。请注意,ABC 在下拉列表中显示为倾斜的红色文本,并且在选择时也会显示在选择框中。

但是,如果我再次打开下拉菜单,所有 3 个项目都将显示为斜红色。

如果选择了 DEF 或 GHI 项目,则这些项目以正常字体显示,黑色并且在打开下拉菜单时再次正确显示 - ABC 仍显示倾斜的红色。

我做错了什么?

【问题讨论】:

    标签: wpf combobox styling


    【解决方案1】:

    这是因为Dependency Property Value Precedence。 When ABC is selected the ComboBoxItems in the DropDown inherit the FontStyle and Foreground from the ComboBox.

    这将修复代码,因为ComboBoxItems 不会从 ComboBox 继承 FontStyle 和 Foreground:

        <Page.Resources>
                <Style x:Key="ItemStyle"
                       TargetType="{x:Type ComboBoxItem}">
                    <Setter Property="FontStyle"
                            Value="Normal" />
                    <Setter Property="Foreground"
                            Value="Black" />
                    <Style.Triggers>
                        <Trigger Property="Content"
                                 Value="ABC">
                            <Setter Property="FontStyle"
                                    Value="Oblique" />
                            <Setter Property="Foreground"
                                    Value="Red" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
                <Style x:Key="BoxStyle"
                       TargetType="{x:Type ComboBox}">
                    <Style.Triggers>
                        <Trigger Property="Text"
                                 Value="ABC">
                            <Setter Property="FontStyle"
                                    Value="Italic" />
                            <Setter Property="Foreground"
                                    Value="Red" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Page.Resources>
            <Border Width="200">
                <ComboBox Style="{StaticResource BoxStyle}"
                          ItemContainerStyle="{StaticResource ItemStyle}"
                          Height="20">
                    <ComboBoxItem>ABC</ComboBoxItem>
                    <ComboBoxItem>DEF</ComboBoxItem>
                    <ComboBoxItem>GHI</ComboBoxItem>
                </ComboBox>
            </Border>
    

    【讨论】:

    • 很好,解决了问题。谢谢你的解释。
    猜你喜欢
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多