【发布时间】:2019-02-20 12:09:04
【问题描述】:
我想单独设计我的 ListViewItems 的选择颜色。我发现了很多操作方法。
但不知何故:似乎根本没有任何效果。 在我当前的示例中,我重新使用了该链接中的示例:How to style ListView selection?
我让它轻松地为 TreeViewItems 工作,但对于 ListViewItems 和 ListBoxItems 都不起作用。
谁能解释一下,我做错了什么? 这是我的 xaml:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Height="200" Width="300">
<Window.Resources>
<Style TargetType="ListViewItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightColorKey}" Color="Red"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrush}" Color="Gray"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red"/>
</Style.Resources>
</Style>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsSelected" Value="true" >
<Setter Property="Foreground" Value="Red" /> <!-- only foreground works -->
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.Triggers>
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightColorKey}" Color="Red"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrush}" Color="Gray"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red"/>
</Style.Resources>
</Style>
<Style TargetType="TreeViewItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightColorKey}" Color="Red"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrush}" Color="Gray"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red"/>
</Style.Resources>
</Style>
</Window.Resources>
<StackPanel>
<ListBox>
<ListBoxItem>Item a 1</ListBoxItem>
<ListBoxItem>Item a 2</ListBoxItem>
</ListBox>
<ListView>
<ListViewItem>Item b 1</ListViewItem>
<ListViewItem>Item b 2</ListViewItem>
</ListView>
<TreeView>
<TreeViewItem Header="Node Level 0">
<TreeViewItem Header="Node Level 1" />
</TreeViewItem>
</TreeView>
</StackPanel>
</Window>
这就是它的外观:
我还尝试了 SystemColors 的所有可用资源键。没有任何效果。这个截图是一个新的 WpfApp 的结果,我没有全局样式。
【问题讨论】: