wpf中如何改变Listbox选中项的颜色
时间: 2024-02-03 13:13:41 浏览: 235
可以使用ListBox.ItemContainerStyle来改变选中项的颜色。可以定义一个新的样式,将其应用于ListBox中的每个项,并在样式中定义选中项的颜色。以下是一个示例:
```
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border x:Name="border" Background="Transparent" BorderBrush="Black" BorderThickness="1">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="border" Property="Background" Value="Yellow" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBoxItem>Item 1</ListBoxItem>
<ListBoxItem>Item 2</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
</ListBox>
```
在这个示例中,我们定义了一个新的ListBoxItem样式,其中包含一个Border控件和一个ContentPresenter。我们使用ControlTemplate.Triggers来定义选中项的背景颜色。当ListBoxItem的IsSelected属性为True时,我们将Border的Background属性设置为Yellow。
请注意,此样式将应用于ListBox中的每个项。如果您只想更改特定项的样式,可以使用ListBox.ItemTemplateSelector来选择不同的模板。
阅读全文
相关推荐















