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>
复制代码

 

posted @   CS讷于言而敏于行  阅读(5318)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
点击右上角即可分享
微信分享提示