为什么当其父控件获得焦点时,ItemsControl 会显示焦点矩形?
当 WPF 的 ItemsControl 认为它具有焦点并且用户按下 Tab 或 Alt 时,它将显示一个焦点矩形。
但我最近有一个 ItemsControl 显示一个焦点矩形,即使它没有焦点 - 它的父级之一有焦点。 ItemsControl 位于 UserControl 内,而 UserControl 又位于另一个具有焦点的 UserControl 内。 像这样的事情:
<!-- UserControl1.xaml; this is the control that has focus -->
<UserControl x:Class="UserControl1" Focusable="True" ...>
<UserControl2/>
</UserControl>
<!-- UserControl2.xaml -->
<UserControl x:Class="UserControl2">
<ItemsControl .../>
</UserControl>
或者,为了直观地显示嵌套:
+---------------------------------------------------+
| UserControl1 (has focus) |
| |
| +-----------------------------------------------+ |
| | UserControl2 | |
| | | |
| | +-------------------------------------------+ | |
| | | ItemsControl (shows focus rectangle) | | |
我花了一段时间(和 StackOverflow 问题)来找出焦点矩形的来源,因为我从来没有想到没有焦点的控件会显示焦点矩形。
我仍在学习 WPF 的方法,显然我还不够了解,否则这不会让我感到困惑。 有两个问题可以帮助我理解:
- 为什么 ItemsControl 实际上没有焦点,但它的父控件之一有焦点时却显示焦点矩形? 产生这个特性的原因是什么? (我确信有一个 - 也许与模板或可视化树有关? - 我只是对 WPF 机制和哲学还没有足够深入的了解。)
- 如何工作? ItemsControl 使用什么机制来决定它应该显示焦点矩形?
WPF's ItemsControl will display a focus rectangle when it thinks it has focus and the user presses Tab or Alt.
But I recently had an ItemsControl display a focus rectangle even though it did not have focus -- one of its parents did. The ItemsControl was inside a UserControl, which was inside another UserControl that did have focus. Something like this:
<!-- UserControl1.xaml; this is the control that has focus -->
<UserControl x:Class="UserControl1" Focusable="True" ...>
<UserControl2/>
</UserControl>
<!-- UserControl2.xaml -->
<UserControl x:Class="UserControl2">
<ItemsControl .../>
</UserControl>
Or, to show the nesting visually:
+---------------------------------------------------+
| UserControl1 (has focus) |
| |
| +-----------------------------------------------+ |
| | UserControl2 | |
| | | |
| | +-------------------------------------------+ | |
| | | ItemsControl (shows focus rectangle) | | |
It took me a while (and a StackOverflow question) to figure out where the focus rectangle was coming from, because I never expected a control that didn't have focus to show a focus rectangle.
I'm still learning my way around WPF, and obviously I don't know enough yet, or this wouldn't have confused me. Two questions to help me understand:
- Why does ItemsControl display a focus rectangle when it doesn't actually have focus, but one of its parent controls does? What's the reason for this feature? (I'm sure there is one -- maybe something to do with templates or visual trees? -- I just don't have a deep enough understanding of WPF mechanics and philosophy yet.)
- How does this work? What's the mechanism ItemsControl uses to decide that it should display a focus rectangle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定 ItemsControl 没有焦点吗? 如果它正在绘制焦点矩形,则应该如此。 仅仅因为视觉树中较高的控件获得焦点,并不意味着它的子控件之一也没有获得焦点。 要了解原因,请确保您已了解 WPF 中“逻辑焦点”和“键盘焦点”之间的区别。 MSDN 上有一个详尽的解释。
Are you sure that the ItemsControl doesn't have focus? If it's drawing a focus rect, it should. Just because a control higher in the visual tree is focused, doesn't mean that one of its children is not also focused. To understand why, make sure you have learned the difference between "logical focus" and "keyboard focus" in WPF. There is an exaustive explanation on MSDN.
我认为这是设计使然。 我的理解是,焦点是从父控件继承的,并沿着视觉树向下移动。 尝试检查 itemscontrol 父级的视觉样式,然后检查 itemscontrol 本身,以查看 xaml 的视觉树发生了什么情况。 这应该会揭示一些关于正在发生的事情的线索。 您也可以尝试在谷歌和其他一些网站上搜索有关焦点和项目控制等的信息。可能还有其他一些信息提供了有关所有工作原理的更多详细信息,这些信息比我可以解释的更清楚。 如果单击 itemscontrol,然后单击其他位置,然后单击父项,会发生什么情况? 焦点有变化吗? 您还可以尝试创建一个覆盖焦点的自定义类,并使控件不会以相同的方式显示焦点。 应该(相当)容易做到。 某些网站/博客上可能有相关信息。
I think this is by design. My understanding is that the focus is inherited from the parent control and works its way down the visual tree. Try examining the visual style of the itemscontrol parent and then the itemscontrol itself to see what is going on with the visual tree of xaml. That should reveal some clues as to what is happening. Also you can try searching google and some other sites for info on focus and itemscontrol, etc. There may be some other info out there that gives more details on how that all works that is clearer than how I can explain it. What happens if you click the itemscontrol and then click somewhere else and then click the parent? Any change in focus? You can also try making a custom class that overrides focus and make it so the control doesnt show focus the same way. Should be (fairly) easy to do. There may be info on that on certain sites/blogs.