奇怪的焦点行为简单的WPF ItemsControl [英] Strange focus behavior for simple WPF ItemsControl

查看:245
本文介绍了奇怪的焦点行为简单的WPF ItemsControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当谈到焦点和键盘导航时,我看到了奇怪的行为。在下面的例子中,我有一个简单的ItemsControl,它已经被模板化了,所以它提供了一个绑定到ItemsSource的CheckBoxes列表。

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

出于某种奇怪的原因,FocusManager.IsFocusScope =True当通过鼠标单击来检查复选框时,键盘焦点未能被设置,并且当使用键盘上的空格键检查复选框时,焦点跳出ItemsControl。这两个症状似乎指向一些奇怪的导航发生时复选框被检查,但我很难进入底部。

如果我使用此方法将可视树中的任何父元素设置为焦点范围。如果我删除了 FocusManager.IsFocusScope =True,那么问题就消失了。不幸的是,我在一个更大的项目中看到了这个问题,我不能去除这些焦点范围而不必担心与焦点相关的其他后果。有人可以向我解释奇怪的行为我正在看?这是一个错误还是我完全错过了什么?

解决方案

这篇文章解释得非常好: http://www.codeproject.com/KB/WPF/EnhancedFocusScope.aspx


FocusScope设计的是什么? 微软在WPF中使用FocusScope
创建临时次要焦点。
WPF中的每个ToolBar和Menu都有自己的
自己的焦点范围。

有了这些知识,我们可以清楚地知道
的原因这些问题:

工具栏按钮本身不应该执行
命令,但是在
被点击之前
已经有了焦点。为了达到这个目的,路由的
命令忽略了焦点
范围内的焦点,而是使用'main'逻辑
焦点。这解释了为什么
路由命令在
焦点范围内不起作用。

为什么
测试应用程序中的大文本框截图仍然
显示一个插入符号?我不知道
的答案 - 但为什么不呢?
当然,文本框没有
键盘焦点(
中的小文本框WPF焦点范围有);但
的主要逻辑焦点仍然是活动窗口
,并且是所有路由命令的接收者

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


为什么键盘焦点移动到
的大文本框,当您选项卡中的
复选框在WPF焦点范围和
按空格切换它?



那么,当你点击一个菜单项或一个
工具栏时,这正是你期望的
:键盘焦点应该
返回到主焦点。所有
ButtonBase派生的控件都会执行
this。



I'm seeing strange behavior when it comes to focus and keyboard navigation. In the example below I have a simple ItemsControl that has been templated so that it presents a list of CheckBoxes bound to the ItemsSource.

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

For some strange reason the FocusManager.IsFocusScope="True" assignment causes keyboard focus to fail to be set when checking a checkbox via a mouse click and for focus to jump out of the ItemsControl when a check box is checked using the space bar on the keyboard. Both symptoms seem to point to some strange navigation happening when the checkbox is checked but I'm having a hard time getting to the bottom of it.

This problem occurs if I set any parent element up the visual tree as a focus scope using this method. If I remove the FocusManager.IsFocusScope="True" then the problems go away. Unfortunately I'm seeing this problem in a larger project where I cannot just remove these focus scopes without worrying about other focus related consequences.

Could somebody explain to me the strange behavior I'm seeing? Is this a bug or am I just completely missing something?

解决方案

This article explains it very well: 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.

And this part covers the behavior you're seeing

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 ItemsControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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