如何一起使用 IsKeyboardFocusWithin 和 IsSelected?
我为 ListBoxItems
定义了一个样式,当 IsSelected
为 True 时,使用触发器设置背景颜色:
<Style x:Key="StepItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Padding="0" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="#40a0f5ff"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
即使 ListBox
时,此样式也会保留所选项目code> 和 ListBoxItem
失去焦点,在我的例子中这是绝对必须的。 问题是,我还希望在 TextBox
的子项之一获得焦点时选择 ListBoxItem
。为了实现此目的,我添加了一个触发器,当 IsKeyboardFocusWithin
为 true 时,将 IsSelected
设置为 true:
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
当我添加此触发器时,当焦点位于子 上时,将选择 Item TextBox
,但第一个行为消失了。现在,当我在 ListBox
外部单击时,该项目将被取消选择。
我怎样才能保持这两种行为?
I have a style defined for my ListBoxItems
with a trigger to set a background color when IsSelected
is True:
<Style x:Key="StepItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Padding="0" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="#40a0f5ff"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This style maintains the selected item even when the ListBox
and ListBoxItem
loses focus, which in my case is an absolute must.
The problem is that I also want the ListBoxItem
to be selected when one of its TextBox
's child gets focused. To achieve this I add a trigger that sets IsSelected
to true when IsKeyboardFocusWithin
is true:
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
When I add this trigger the Item is selected when the focus is on a child TextBox
, but the first behaviour disappears. Now when I click outside the ListBox
, the item is de-selected.
How can I keep both behaviours?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您的列表框失去焦点时,它会将所选项目设置为空,因为您的触发器。您可以使用一些隐藏的代码来选择焦点,当您失去焦点时,这些代码不会取消选择。
XAML:
隐藏代码:
When your listbox looses focus, it will set selected item to null because of your trigger. You can select on focus using some code behind that will not unselect when you loose focus.
XAML:
Code behind:
“当我添加此触发器时,当焦点位于子文本框上时,会选择该项目,但第一个行为会消失。现在,当我在列表框外部单击时,该项目将被取消选择。”
事实上,我不认为它已经失去了原来的行为。我怀疑正在发生的事情是您从其他地方直接单击文本框,因此底层的 ListBoxItem 实际上从未被选中。但是,如果确实如此,您会发现在您按照自己的意愿离开后,该选择仍然会保留。
您可以通过直接单击 ListBoxItem 来强制选择它来测试这一点(旁注:您应该始终给它一个背景,即使只是“透明”,这样它就可以接收鼠标点击,如果它为 null,则不会) )或者甚至只需按“Shift-Tab”即可将焦点设置为从文本框返回。
但是,这并不能解决您的问题,即 TextBox 获得焦点但不让底层 ListBoxItem 知道它。
您可以使用两种方法:事件触发器或附加行为。
第一个是 IsKeyboardFocusWithinChanged 事件上的事件触发器,如果键盘焦点更改为 true,则将“IsSelected”设置为 true。 (注意:Sheridan 的答案做了一个虚假更改通知,但它不应该在您可以在列表中进行多项选择的情况下使用,因为所有内容都会被选中。)但即使是事件触发器也会导致问题,因为您会失去多选行为例如切换或范围单击等。
另一种(也是我的首选方法)是编写在 ListBoxItem 上设置的附加行为,可以直接设置,也可以通过样式(如果您愿意)设置。
这是附加的行为。注意:如果您想实现它,您将再次需要处理多选内容。另请注意,虽然我将行为附加到 ListBoxItem,但在内部我将其强制转换为 UIElement。这样,您还可以在 ComboBoxItem、TreeViewItem 等中使用它。基本上是基于选择器的控件中的任何 ContainerItem。
...并且您只需将其添加为 ListBoxItem 样式中的属性设置器。
这当然假设您已经导入了一个名为“behaviors”的 XML 命名空间,该命名空间指向包含该类的命名空间。您可以将类本身放入共享的“Helper”库中,这就是我们所做的。这样,无论我们需要什么,它都是在 XAML 中设置的简单属性,而行为则负责处理其他所有事情。
"When I add this trigger the Item is selected when the focus is on a child TextBox, but the first behaviour disappears. Now when I click outside the ListBox, the item is de-selected."
Actually, I don't think it has lost that original behavior. What I suspect is happening is you're clicking directly in the textbox from somewhere else so the underlying ListBoxItem never actually became selected. If it did however, you'd see the selection would still remain after you left as you want.
You can test this by forcing the ListBoxItem to be selected by clicking directly on it (side-note: you should always give it a background, even if just 'transparent' so it can receive mouse clicks, which it won't if it's null) or even just hitting 'Shift-Tab' to set the focus there, back from the textbox.
However, that doesn't solve your issue, which is that the TextBox gets the focus but doesn't let the underlying ListBoxItem know about it.
The two approaches you can use for that are an event trigger or an attached behavior.
The first is an event trigger on the IsKeyboardFocusWithinChanged event where you set 'IsSelected' to true if the keyboard focus changed to true. (Note: Sheridan's answer does a faux-change-notification but it should not be used in cases where you can multi-select in the list because everything becomes selected.) But even an event trigger causes issues because you lose the multi-select behaviors such as toggling or range-clicking, etc.
The other (and my preferred approach) is to write an attached behavior which you set on the ListBoxItem, either directly, or via a style if you prefer.
Here's the attached behavior. Note: You again would need to handle the multi-select stuff if you want to implement that. Also note that although I'm attaching the behavior to a ListBoxItem, inside I cast to UIElement. This way you can also use it in ComboBoxItem, TreeViewItem, etc. Basically any ContainerItem in a Selector-based control.
...and you simply add this as a property setter in your ListBoxItem's style
This of course assumes you've imported an XML namespace called 'behaviors' that points to the namespace where the class is contained. You can put the class itself in a shared 'Helper' library, which is what we do. That way, everywhere we want it, its a simple property set in the XAML and the behavior takes care of everything else.
我发现
IsKeyboardFocusWithin
不是最好的解决方案。在本例中,我所做的是在用作 DataTemplate 的所有控件上设置样式,以发送要在后面的代码中处理的
GotFocus
事件。然后,在代码隐藏中,我搜索可视化树(使用 VisualTreeHelper)以找到 ListViewItem 并将IsSelected
设置为true< /代码>。这样,它就不会“接触”DataContext,而仅适用于 View 元素。
I figured out that
IsKeyboardFocusWithin
is not the best solution.What I did in this case was to set the style on all of the controls used as DataTemplate to send the
GotFocus
-event to be handled in code behind. Then, in code behind, I searched up the visual tree (usingVisualTreeHelper
) to find theListViewItem
and setIsSelected
totrue
. This way it does not "touch" the DataContext and works just with the View elements.