.net 将选定的和未聚焦的列表框样式更改为不灰显
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/698830/
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
Change selected and unfocused Listbox style to not be grayed out
提问by Ray
I have a really simple WPF ListBox with SelectionMode set to Multiple.
我有一个非常简单的 WPF ListBox,其中 SelectionMode 设置为 Multiple。
<ListBox SelectionMode="Multiple" />
When the ListBox loses focus it's really hard to tell what's been selected because the selection colour changes from blue to a light grey colour. What's the easiest way of changing this behaviour so that it stays blue?
当 ListBox 失去焦点时,很难判断选择了什么,因为选择颜色会从蓝色变为浅灰色。改变这种行为使其保持蓝色的最简单方法是什么?
I know it's probably something to do with the ListItem's style, but I can't find where.
我知道这可能与 ListItem 的样式有关,但我找不到在哪里。
Cheers.
干杯。
回答by Guy Starbuck
I have done something like this using the following in a merged ResourceDictionary, it may help you:
我在合并的 ResourceDictionary 中使用以下内容完成了类似的操作,它可能对您有所帮助:
<Style TargetType="ListBoxItem">
<Style.Resources>
<!--SelectedItem with focus-->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue" Opacity=".4"/>
<!--SelectedItem without focus-->
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey }" Color="LightBlue" Opacity=".4"/>
</Style.Resources>
</Style>
回答by Eben Geer
This is not an answer to the question, but I found this when I was looking for a way to disable selections in my listboxes. By using a slightly modified form of Guy's & bendewey's technique above, it turns out you can give the appearance of no selections in your listbox without replacing the underlying items control or anything like that. Here's the code I used:
这不是问题的答案,但我在寻找禁用列表框中选择的方法时发现了这一点。通过使用上面 Guy's & Bendewe's 技术的稍微修改形式,结果表明您可以在列表框中显示没有选择的外观,而无需替换底层项目控件或类似的东西。这是我使用的代码:
<Grid.Resources>
<Style TargetType="ListBoxItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="White" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="White" />
</Style.Resources>
</Style>
</Grid.Resources>
I also found the following MSDN page helpful:
我还发现以下 MSDN 页面很有帮助:
MSDN: SystemColors Members (System.Windows)
MSDN:SystemColors 成员 (System.Windows)
Thanks for the help, guys.
谢谢你们的帮助,伙计们。
回答by nmclean
A more complete solution would be to have the new brush refer to HighlightColor:
更完整的解决方案是让新画笔参考 HighlightColor:
<Style TargetType="ListBox">
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ListBoxItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static Member=SystemColors.InactiveSelectionHighlightBrushKey}"
Color="{DynamicResource ResourceKey={x:Static Member=SystemColors.HighlightColorKey}}" />
</Style.Resources>
</Style>
</Setter.Value>
</Setter>
</Style>
This would ensure that it uses the same color and matches the system theme (even if the system theme changes during runtime, thanks to DynamicResource).
这将确保它使用相同的颜色并匹配系统主题(即使系统主题在运行时发生变化,感谢 DynamicResource)。
By the way, it seems recent versions of WPF do not use "ControlBrush" for this anymore, but the more appropriate "InactiveSelectionHighlightBrush".
顺便说一句,似乎最近版本的 WPF 不再为此使用“ControlBrush”,而是使用更合适的“InactiveSelectionHighlightBrush”。
回答by bendewey
You can probably solve your problem by re-writing the Template, but try this for an easy patch.
您可能可以通过重新编写模板来解决您的问题,但请尝试此操作以获得简单的补丁。
<Style TargetType="ListViewItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Blue" />
</Style.Resources>
</Style>
回答by ncfuncion
I also have this problem. But I solved this by using IsSynchronizedWithCurrentItem="True":
我也有这个问题。但我通过使用 IsSynchronizedWithCurrentItem="True" 解决了这个问题:
<ListBox
IsSynchronizedWithCurrentItem="True" />
回答by Sachar
When your selected items become grayed out it is because your control is losing its logical focus. Multiple controls have the ability to have logical focus at the same time. So a simple solution to this could be to give your ListBox a FocusScope via the FocusManager.
当您选择的项目变灰时,这是因为您的控件正在失去其逻辑焦点。多个控件可以同时拥有逻辑焦点。因此,对此的一个简单解决方案可能是通过 FocusManager 为您的 ListBox 提供一个 FocusScope。
<ListBox SelectionMode="Multiple" FocusManager.IsFocusScope="True"></ListBox>

