wpf combobox设置选中项背景颜色

今天刚好用到这个功能,记录下
博主写了个demo:https://github.com/xdqt/Wpf-datagrid-treeview.git

<ComboBox x:Name="combobox" ItemsSource="{Binding Grades}" SelectedItem="{Binding Kc}" IsEditable="True" Text="{Binding Edit}" LostFocus="Combobox_LostFocus">
            <ComboBox.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">Red</SolidColorBrush>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}">Blue</SolidColorBrush>
            </ComboBox.Resources>
        </ComboBox>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过设置ComboBox的ItemContainerStyle属性来实现下拉框选中行的背景颜色。具体步骤如下: 1. 在ComboBox中添加ItemContainerStyle属性,指定一个Style,如下所示: ``` <ComboBox ItemContainerStyle="{StaticResource ComboBoxItemStyle}"> <ComboBoxItem>选1</ComboBoxItem> <ComboBoxItem>选2</ComboBoxItem> <ComboBoxItem>选3</ComboBoxItem> </ComboBox> ``` 2. 在Window或者Application的Resources中定义一个Style,名为ComboBoxItemStyle,如下所示: ``` <Window.Resources> <Style x:Key="ComboBoxItemStyle" TargetType="ComboBoxItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ComboBoxItem}"> <Border Name="Border" Padding="2" SnapsToDevicePixels="true" Background="{TemplateBinding Background}"> <ContentPresenter /> </Border> <ControlTemplate.Triggers> <Trigger Property="IsHighlighted" Value="true"> <Setter TargetName="Border" Property="Background" Value="LightBlue" /> </Trigger> <Trigger Property="IsSelected" Value="true"> <Setter TargetName="Border" Property="Background" Value="LightGreen" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> ``` 其中,IsHighlighted和IsSelected分别表示鼠标悬停和被选中。在上述代码中,当IsHighlighted为true时,设置的背景为LightBlue;当IsSelected为true时,设置的背景为LightGreen。 通过以上步骤,就可以实现ComboBox下拉框选中行的背景颜色了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值