不在焦点时覆盖 ListBoxItem 背景颜色 (.NET 4.5) [英] Overriding ListBoxItem background color when not in focus (.NET 4.5)
问题描述
根据这个,覆盖 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?
这似乎与 .net 4.5 下的列表/组合框背景和选定颜色.
推荐答案
Try the following for changing the selected ListBoxItem's background color when it has lost focus:
Try the following for changing the selected ListBoxItem's background color when it has lost focus:
XAML
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey }" Color="Orange" />
</ListBox.Resources>
C#
listBox.Resources.Add(SystemColors.InactiveSelectionHighlightBrushKey,
new SolidColorBrush(Colors.Orange));
希望对你有用.
这篇关于不在焦点时覆盖 ListBoxItem 背景颜色 (.NET 4.5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!