wpf中如何改变Listbox选中项的颜色
时间: 2023-11-27 08:55:00 浏览: 152
在WPF中,可以通过修改ListBox控件的样式来改变选中项的颜色。
以下是一种实现方式:
1. 在ListBox控件中添加以下代码:
```
<ListBox Name="myListBox">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border Name="Border" Padding="2" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="Red" />
<Setter Property="Foreground" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
```
2. 在上述代码中,我们使用了ListBox控件的ItemContainerStyle属性来定义ListBoxItem选中时的样式。在样式中,我们使用了ControlTemplate来定义ListBoxItem的外观,使用Trigger来定义选中状态下的背景色和前景色。
在上述代码中,选中的项的背景色为红色,前景色为白色。你可以根据需要修改这些颜色。
希望这可以帮助到你。
阅读全文
相关推荐
















