WPF:确定面板是否对用户可见
我在选项卡(WPF 应用程序)中有一个 WPF 用户控件(myGraphicControl)。
当表单大小发生变化时,我会在 myGraphicControl 中重新绘制图形。
由于重绘操作是我只需要在可见选项卡中的控件中执行的操作。
WPF(用户)控件如何检测它实际上是否“可见”?
附言。
可见我的意思是用户可以看到它。 比如说,如果可见文本框位于当前不可见的选项卡中,则用户看不到该文本框。
I have a WPF usercontrol (myGraphicControl) in a tab (WPF application).
When the form size changes, I redraw the graph in myGraphicControl.
Since the redrawing operation is a I need to do it only the control in in the visible tab.
How the WPF (user)control can detect if it's "visible" actually or not?
PS.
by Visible I mean that user can see it.
say, if a Visible TextBox is located in the currently invisible tab, this textBox is not visible by the user.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不相信这里有快速修复解决方案,但您也许可以使用
UIElement.InputHitTest(Point)
。您可以进行类似的调用
但是,我应该指出,我自己没有尝试过,并且它可能在以下情况下不起作用:
I don't believe there is a quick-fix solution here, but you may be able to do something using
UIElement.InputHitTest(Point)
.You could make a call similar to
However, I should point out that I haven't tried this myself, and that it probably won't work in the following scenarios:
我发现虽然史蒂夫的方法通常有效,但如果您从子控件中间的某个位置获得一个点,它的工作会更可靠。我猜测可能布局舍入的某个地方使得 InputHitTest 检查有些不精确。因此,将他的第一行更改为以下内容,你就成功了:
I've found that while Steve's method generally works, it works much more reliably if you get a point from somewhere in the middle of the child control. I'm guessing that maybe layout rounding somewhere along the way makes the InputHitTest check somewhat inexact. So, change his first line to the following and you're golden:
也许 UIElement.IsVisible 会有所帮助?它适用于选项卡内容。
无论如何,您可以使用描述的解决方案 在这里。
我还有一种解决方案。 TabControl 的当前实现从可视化树中删除非活动选项卡。因此,确定元素是否可见的另一种方法是查找PresentationSource。对于非活动选项卡的元素,它将为空。
Maybe UIElement.IsVisible will be helpful? It works for tab contents well.
Anyway you can use a solution described here.
I have one more solution. The current implementation of TabControl removes inactive tabs from visual tree. So, another way to determine whether your element is visible is to find PresentationSource. It will be null for elements of inactive tabs.