WPF中修改ListBox项的样式病修改选中项的背景颜色

最终效果:

  

 1         <ListBox Name="cmb">
 2             <!--修改颜色-->
 3             <ListBox.Resources>
 4                 <!--高亮背景色-->
 5                 <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
 6                 <!--非高亮背景色-->
 7                 <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red"/>
 8                 <!--高亮文本色-->
 9                 <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
10                 <!--非高亮文本色-->
11                 <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Green"/>
12                 <Style TargetType="ListBox">
13                     <!--重定义ListBox中项的样式-->
14                     <Setter Property="ItemTemplate">
15                         <Setter.Value>
16                             <DataTemplate>
17                                 <Grid Margin="0" Width="150">
18                                     <Border Margin="5" BorderBrush="SteelBlue" BorderThickness="1" CornerRadius="5" Background="{Binding Path=Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}">
19                                         <Grid Margin="5">
20                                             <Grid.RowDefinitions>
21                                                 <RowDefinition/>
22                                                 <RowDefinition/>
23                                             </Grid.RowDefinitions>
24                                             <TextBlock Text="{Binding ID}"/>
25                                             <TextBlock Grid.Row="1"  Text="{Binding Name}"/>
26                                         </Grid>
27                                     </Border>
28                                 </Grid>
29                             </DataTemplate>
30                         </Setter.Value>
31                     </Setter>
32                     <!--自定义选中项的颜色-->
33                     <Setter Property="ItemContainerStyle">
34                         <Setter.Value>
35                             <Style TargetType="ListBoxItem">
36                                 <Style.Triggers>
37                                     <Trigger Property="ListBoxItem.IsSelected" Value="True">
38                                         <Setter Property="ListBoxItem.Background" Value="Green"/>
39                                     </Trigger>
40                                 </Style.Triggers>
41                             </Style>
42                         </Setter.Value>
43                     </Setter>
44                 </Style>
45             </ListBox.Resources>
46         </ListBox>

 

转载于:https://www.cnblogs.com/cs569/p/8507758.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值