gpt4 book ai didi

wpf - 简单 WPF ItemsControl 的奇怪焦点行为

转载 作者:行者123 更新时间:2023-12-04 16:21:42 25 4
gpt4 key购买 nike

在焦点和键盘导航方面,我看到了奇怪的行为。在下面的示例中,我有一个简单的 ItemsControl,它已被模板化,以便它显示绑定(bind)到 ItemsSource 的 CheckBox 列表。

<ItemsControl FocusManager.IsFocusScope="True"
ItemsSource="{Binding ElementName=TheWindow, Path=ListOStrings}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

由于某些奇怪的原因,FocusManager.IsFocusScope="True"分配导致通过鼠标单击检查复选框时无法设置键盘焦点,并且在使用空格键检查复选框时焦点跳出 ItemsControl键盘。这两种症状似乎都表明在选中复选框时发生了一些奇怪的导航,但我很难深入了解它。

如果我使用此方法将可视树上的任何父元素设置为焦点范围,则会出现此问题。如果我删除 FocusManager.IsFocusScope="True"那么问题就消失了。不幸的是,我在一个更大的项目中看到了这个问题,我不能只删除这些焦点范围而不担心其他与焦点相关的后果。

有人可以向我解释我看到的奇怪行为吗?这是一个错误还是我完全错过了什么?

最佳答案

这篇文章解释的很好:http://www.codeproject.com/KB/WPF/EnhancedFocusScope.aspx

For What was FocusScope Designed?

Microsoft uses FocusScope in WPF to create a temporary secondary focus. Every ToolBar and Menu in WPF has its own focus scope.

With this knowledge, we can clearly see why we have those problems:

A toolbar button should not execute commands on itself, but on whatever had focus before the toolbar was clicked. To accomplish this, routed commands ignore the focus from focus scopes and use the 'main' logical focus instead. This explains why routed commands don't work inside focus scopes.

Why does the large text box in the test application screenshot still display a caret? I don't know the answer to this - but why shouldn't it? Granted, the text box doesn't have the keyboard focus (the small text box in the WPF focus scope has that); but it still has the main logical focus in the active Window and is the receiver of all routed commands.



这部分涵盖了您所看到的行为

Why does the keyboard focus move to the large text box when you tab to the CheckBox in the WPF focus scope and press Space to toggle it?

Well, this is exactly what you expect when you click a menu item or a toolbar: the keyboard focus should return to the main focus. All ButtonBase-derived controls will do this.

关于wpf - 简单 WPF ItemsControl 的奇怪焦点行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4954253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com