[英]How to change a listviewitems highlighted colour in WPF
I'm simply trying to change the highlighted colour of a listviewitem in WPF.我只是想在 WPF 中更改 listviewitem 的突出显示颜色。 The solutions I've found online including Stackoverflow are having no effect on my listview.
我在网上找到的包括 Stackoverflow 在内的解决方案对我的列表视图没有影响。 Am I going crazy?
我要疯了吗? Am I missing something?
我错过了什么吗? Please show me how to do this.
请告诉我如何做到这一点。 Here is my example code.
这是我的示例代码。 The items are still showing the default blue.
这些项目仍然显示默认的蓝色。
<Window.Resources>
<Style TargetType="ListViewItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static
SystemColors.HighlightBrushKey}" Color="Red" />
</Style.Resources>
</Style>
</Window.Resources>
<Grid>
<ListView VerticalAlignment="Top" Background="#2e2e2e"
Foreground="White">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static
SystemColors.HighlightTextBrushKey}"
Color="Red" />
<SolidColorBrush x:Key="{x:Static
SystemColors.HighlightBrushKey}"
Color="Purple" />
</Style.Resources>
</Style>
</ListView.ItemContainerStyle>
<ListViewItem Content="ITEM" />
<ListViewItem Content="ITEM" />
<ListViewItem Content="ITEM" />
<ListViewItem Content="ITEM" />
<ListViewItem Content="ITEM" />
</ListView>
<Button VerticalAlignment="Bottom" Height="50" />
</Grid>
You have to override the ControlTemplate
of the ListViewItem
.您必须覆盖
ListViewItem
的ControlTemplate
。 This way you can also design apart form the highlighting any other visual user interaction behavior and create transition animations.通过这种方式,您还可以设计突出显示任何其他视觉用户交互行为并创建过渡动画。
<Style TargetType="ListBoxItem">
<Style.Resources>
<SolidColorBrush x:Key="HighlightTextBrushKey"
Color="Red" />
<SolidColorBrush x:Key="HighlightBrushKey"
Color="Purple" />
<SolidColorBrush x:Key="HighlightMouseOverBrushKey"
Color="{Binding Source={StaticResource HighlightBrushKey}, Path=Color}"
Opacity="0.3" />
</Style.Resources>
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Padding="{TemplateBinding Padding}"
Margin="{TemplateBinding Margin}">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource HighlightBrushKey}"/>
<Setter Property="Foreground" Value="{StaticResource HighlightTextBrushKey}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource HighlightMouseOverBrushKey}"/>
<Setter Property="Foreground" Value="{StaticResource HighlightTextBrushKey}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
An alternative to the triggers you can animate transitions with the help of the VisualStateManager
可以在
VisualStateManager
的帮助下为过渡设置动画的触发器的替代方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.