当列表失去焦点时 WPF SelectedItem 颜色消失 [英] WPF SelectedItem color disappears when the list loses the focus

查看:32
本文介绍了当列表失去焦点时 WPF SelectedItem 颜色消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF Window,它基本上有一个 ListBox 显示项目列表.

I have a WPF Window which basically has a ListBox which displays a list of items.

然后我有一个 ListView 基本上显示 ListBoxSelectedItem 的详细信息.

I then have a ListView basically displaying the details of the SelectedItem in the ListBox.

问题是,当焦点不再位于 ListBox 上时,突出显示的颜色就会消失,我再也看不到选择了哪个主项.

The thing is, when the focus is not on the ListBox anymore, the highlighting color disappears and I cannot see which master item was selected anymore.

你知道我该如何解决这个问题吗?(即确保该项目保持突出显示)

Do you know how I can solve this? (ie. making sure that the item stays highlighted)

推荐答案

最快的方法是使用 ListBoxItem 上的样式来覆盖默认的系统颜色:

The quickest way is to use a style on the ListBoxItem to override the default System colors:

<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.ControlBrushKey}" 
         Color="LightBlue" Opacity=".4"/>
   </Style.Resources>
</Style>

这是为列表项定义 ItemTemplate 的稍微复杂(但更容易控制)方法的一种快捷方式.网上有很多这样的例子,我就不放在这里了.

This is kind of a shortcut to the slightly more complex (but easier to control) method of defining the ItemTemplate for the list items. There are plenty of examples of that online, so I won't put that here.

这篇关于当列表失去焦点时 WPF SelectedItem 颜色消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆