为什么WPF ComboBox样式不如预期?

时间:2012-10-17 13:54:27

标签: wpf combobox styling

我有一个带有ComboBox的WPF应用程序,我想要对项目进行样式设置,但根据选择的项目会产生意外行为。

以下XAML代码段演示了此问题:

<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仍显示倾斜的红色。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

这是因为Dependency Property Value Precedence。选择ABC后,DropDown中的ComboBoxItem会从FontStyle继承ForegroundComboBox

这将修复代码,因为ComboBoxItem不会从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>