更改wpf中选定的未聚焦组合框项的颜色

更改wpf中选定的未聚焦组合框项的颜色,wpf,xaml,Wpf,Xaml,我想更改组合框中SelectedUnfocused(下图中用红色边框标记的项目)项目的颜色 因此,我尝试更改组合框的控件模板 从MSDN复制ControlTemplate之前: 从MSDN复制ControlTemplate后: 所以,这个模板改变了我控件的整个外观。我不想改变那些颜色 要获取我使用的模板。如果只想更改ComboBoxItem样式,而不是ComboBox样式,只需从XAML中删除style TargetType=“ComboBox”,只保留相关的样式TargetType=“Co

我想更改组合框中
SelectedUnfocused(下图中用红色边框标记的项目)
项目的颜色

因此,我尝试更改组合框的控件模板

从MSDN复制ControlTemplate之前:

从MSDN复制ControlTemplate后:

所以,这个模板改变了我控件的整个外观。我不想改变那些颜色


要获取我使用的模板。

如果只想更改
ComboBoxItem
样式,而不是
ComboBox
样式,只需从XAML中删除
style TargetType=“ComboBox”
,只保留相关的
样式TargetType=“ComboBoxItem
来自链接的MSDN页面。

仅添加链接页面中的
ComboxItem
样式和样式中使用的
相关笔刷
。不需要覆盖完整的模板

在XAML中只复制/粘贴这么多代码,您就可以开始了:

<Color x:Key="SelectedBackgroundColor">#FFC5CBF9</Color>
<Color x:Key="SelectedUnfocusedColor">#FFDDDDDD</Color> <-- Update color here

<Style x:Key="{x:Type ComboBoxItem}"
       TargetType="{x:Type ComboBoxItem}">
  <Setter Property="SnapsToDevicePixels"
          Value="true" />
  <Setter Property="OverridesDefaultStyle"
          Value="true" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type ComboBoxItem}">
        <Border x:Name="Border"
                Padding="2"
                SnapsToDevicePixels="true"
                Background="Transparent">
          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="SelectionStates">
              <VisualState x:Name="Unselected" />
              <VisualState x:Name="Selected">
                <Storyboard>
                  <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                Storyboard.TargetProperty="(Panel.Background).
                    (SolidColorBrush.Color)">
                    <EasingColorKeyFrame KeyTime="0"
                                         Value="{StaticResource SelectedBackgroundColor}" />
                  </ColorAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="SelectedUnfocused">
                <Storyboard>
                  <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                Storyboard.TargetProperty="(Panel.Background).
                    (SolidColorBrush.Color)">
                    <EasingColorKeyFrame KeyTime="0"
                                         Value="{StaticResource SelectedUnfocusedColor}" />
                  </ColorAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
          <ContentPresenter />
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>
#FFC5CBF9

#在看到HighCore给出的答案后,我刚刚尝试过。如果我这样做,或者如果我粘贴整个控件模板,我会发现一个问题。我把鼠标盖的颜色弄丢了。所以,现在如果我将鼠标移到ComboBox中的任何项目上,它的颜色是透明/白色的。我怎么能换呢?我有。我必须使用name=CommonStates的新VisualStateGroup,在此情况下,我必须定义两个名为Normal和MouseOver的视觉状态。里面我已经改变了画笔的颜色。我还使用了您提到的上述代码。谢谢你再次解决了我的问题。