改变 ListBox Item 被点击后的蓝色背景

WPF 里,ListBox 的 Item 被点击后 ,背景是 蓝色的。

有时候我们不想要蓝色,想要根据项目环境来决定颜色,可以像下面这样做。

<Style x:Key="lbiOri" TargetType="{x:Type ListBoxItem}">
    <Style.Resources>
        <SolidColorBrush Color="LightGreen" x:Key="{x:Static SystemColors.HighlightBrushKey}"/>
         <SolidColorBrush Color="Black" x:Key="{x:Static SystemColors.HighlightTextBrushKey}"/>
    </Style.Resources>
</Style>

Style 的 TargetType 指定为 ListBoxItem。

SystemColors.HighlightBrushKey 指定绘制选定项的背景的对象为该 SolidColorBrush。

SystemColors.HighlightTextBrushKey 指定绘制选定项的文字的对象为该 SolidColorBrush。

接着,我们只需要指定 ItemContainerStyle 为定义的资源即可。

<ListBox ItemContainerStyle="{StaticResource lbiOri}"/>
ItemContainerStyle 指定了 Item 的 Panel。





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值