问题描述
根据,重写ControlBrushKey资源应在没有焦点的情况下更改ListBox选定项的背景颜色。我创建了一个简单的示例来证明这一点:
According to this, overriding the ControlBrushKey resource should change the background color of a ListBox selected item when it doesn't have focus. I created a simple example to disprove this:
<StackPanel>
<ListBox>
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
<!--SelectedItem without focus but doesn't really work-->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Orange" />
</ListBox.Resources>
<ListBoxItem>
Item 1
</ListBoxItem>
<ListBoxItem>
Item 2
</ListBoxItem>
</ListBox>
<TextBox></TextBox>
</StackPanel>
如果您在.NET 4.5中运行此程序,则可以看到它只会更改焦点对准的颜色,但不是重点(它在.NET 4.0中有效)。知道为什么吗?
If you run this in .NET 4.5 you can see that it only changes the in-focus color, but not the not-in-focus (it works in .NET 4.0). Any idea why?
编辑:这似乎与。
This seems to be duplicate of List/Combo Box Background And Selected Colours Under .net 4.5.
推荐答案
尝试以下操作来更改选定的ListBoxItem失去焦点时的背景颜色:
Try the following for changing the selected ListBoxItem's background color when it has lost focus:
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey }" Color="Orange" />
</ListBox.Resources>
listBox.Resources.Add(SystemColors.InactiveSelectionHighlightBrushKey,
new SolidColorBrush(Colors.Orange));
这篇关于不清晰时覆盖ListBoxItem背景色(.NET 4.5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!