如何根据自定义控件的任何子控件是否获得焦点来设计自定义控件的样式?
我们有一个自定义画布,其中有专门的节点,其行为与标准 MDI 应用程序的窗口非常相似。期望的行为是,如果“窗口”的任何子控件具有焦点,则该“窗口”被认为是活动的。
现在 IsFocused 属性似乎没有级联,这意味着如果子控件具有焦点,它的容器也不会设置为“焦点”,因此我们无法使用它。出于同样的原因,我们无法在容器上设置 IsFocused 属性,因为我相信这会从子级中窃取它。
我唯一的想法是创建一个名为 HasChildWithFocus 或类似名称的新 DP,然后在代码隐藏中侦听冒泡事件并设置该标志。不确定这是最好的方法。 (我们可以将其实现为附加属性/附加行为的组合。)
但是,如果我们可以简单地询问控件“嘿......您或您的任何孩子有焦点吗?”,当然会更好。
那么你可以吗?
We have a custom canvas which has specialized nodes that behave a lot like a standard MDI application's windows. The desired behavior is that if any of the child controls of the "window" have the focus, then that "window" is said to be active.
Now the IsFocused property doesn't seem to cascade, meaning if a child control has the focus, it's container is not also set to 'focused' so we can't use that. For the same reason, we can't set the IsFocused property on the container as I believe that would steal it from the child.
My only thought is to create a new DP called HasChildWithFocus or something like that, then in the code-behind, listen for the bubbled events and set that flag. Not sure that's the best way to go. (We may implement that as a combination attached property/attached behavior kinda thing.)
But of course it would be much better if we could simply ask a control 'Hey... do you or any of your children have the focus?'
So can you?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以像这样直接使用
UIElement.IsKeyboardFocusWithin
:在此示例中,包含具有键盘焦点的元素的边框使用不同的边框画笔进行样式设置。
You can use
UIElement.IsKeyboardFocusWithin
directly like this:In this example the border that contains the element with the keyboard focus is styled with a different border brush.