WPF ListView - 如何设置所选项目的背景颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1153345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:44:47  来源:igfitidea点击:

WPF ListView - how do i set selected item background colour?

wpflistviewbackground

提问by monkey_p

I currently have this, but it only work for the foreground colour.

我目前有这个,但它只适用于前景色。

Any help would be apriciated :D

任何帮助都会被感谢:D

<Style.Triggers>
    <Trigger Property="IsSelected" Value="true">
        <Setter Property="Foreground" Value="Red" />
        <Setter Property="Background" Value="Green"/> 
    </Trigger>
</Style.Triggers>

回答by Nir

This will take care of the background color, maybe it will also help you find the solution for the foreground, this is from http://blogs.msdn.com/wpfsdk/archive/2007/08/31/specifying-the-selection-color-content-alignment-and-background-color-for-items-in-a-listbox.aspx

这将处理背景颜色,也许它还可以帮助您找到前景的解决方案,这是来自http://blogs.msdn.com/wpfsdk/archive/2007/08/31/specifying-the-selection -color-content-alignment-and-background-color-for-items-in-a-listbox.aspx

<Style TargetType="ListBoxItem">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green"/>
    </Style.Resources>
</Style>

回答by Kenan E. K.

It is the Bordersub-element of the ListViewItem named "Bd" which handles background painting.

它是名为“Bd”的 ListViewItem的Border子元素,用于处理背景绘画。

<Setter TargetName="Bd" Value="Green" Property="Background" />