【发布时间】:2020-10-14 20:36:58
【问题描述】:
这个问题已经在这里被问和回答了大约十几次,但给定的答案似乎都不起作用。问题:在 WPF ListBox 中,如何更改(所选项目和/或“鼠标悬停”项目的)突出显示颜色?
这不起作用(根据here) - 突出显示保持默认:
<StackPanel>
<StackPanel.Resources>
<Style x:Key="ListBoxSelectedItemsStyle" TargetType="{x:Type ListBoxItem}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Black" />
</Style.Resources>
</Style>
</StackPanel.Resources>
<ListBox ItemContainerStyle="{StaticResource ListBoxSelectedItemsStyle}">
<ListBoxItem>Item1</ListBoxItem>
<ListBoxItem>Item2</ListBoxItem>
<ListBoxItem>Item3</ListBoxItem>
</ListBox>
</StackPanel>
这也不起作用(根据here 略有不同):
<StackPanel>
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow"/>
</Style.Resources>
</Style>
</ListBox.ItemContainerStyle>
<ListBoxItem>Item1</ListBoxItem>
<ListBoxItem>Item2</ListBoxItem>
<ListBoxItem>Item3</ListBoxItem>
</ListBox>
</StackPanel>
这也不起作用(根据here):
<StackPanel>
<ListBox>
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Black"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Black"/>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Resources>
<ListBoxItem>Item1</ListBoxItem>
<ListBoxItem>Item2</ListBoxItem>
<ListBoxItem>Item3</ListBoxItem>
</ListBox>
</StackPanel>
许多答案都包含一些变体(即here 等) - 但无论我使用哪个,都没有任何变化。突出显示不受影响。
【问题讨论】:
-
有趣。那更近了——我确实看到了黄色背景——但它搞砸了项目的其他方面(即现在它们居中而不是左对齐,它们没有填满宽度等)。所以它似乎改变的不仅仅是背景。
-
不适合我.....
-
默认情况下,高亮和可点击区域占据列表框的整个宽度。这样,它只会突出显示文本本身,并且您只能在文本上单击鼠标右键,从而使单击区域明显变小并且项目的功能行为不标准。这不是它为你做的吗?如果不清楚,我可以发布比较屏幕截图...
-
如前所述,对我来说很好。当然只有 ItemContainerStyle,而不是 XAML 的其余部分。