WPF:确定面板是否对用户可见

发布于 2024-10-30 18:02:23 字数 247 浏览 0 评论 0原文

我在选项卡(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫瑾少女 2024-11-06 18:02:23

我不相信这里有快速修复解决方案,但您也许可以使用 UIElement.InputHitTest(Point)

您可以进行类似的调用

//get the coordinates of the top left corner of the child control within
//the parent
var childTopLeft = childControl.TranslatePoint(new Point(), parentControl);
//check whether or not the child control is returned when you request the element
//at that coordinate through hit testing
var isVisible = (parentControl.InputHitTest(childTopLeft) == childControl);

但是,我应该指出,我自己没有尝试过,并且它可能在以下情况下不起作用:

  • 透明项目 - 通常,透明背景会导致控件的命中测试通过到父级
  • 部分遮挡项目 - 您一次只能命中测试一个点,因此如果子控件只有一部分可见,您将必须检查正确的点

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

//get the coordinates of the top left corner of the child control within
//the parent
var childTopLeft = childControl.TranslatePoint(new Point(), parentControl);
//check whether or not the child control is returned when you request the element
//at that coordinate through hit testing
var isVisible = (parentControl.InputHitTest(childTopLeft) == childControl);

However, I should point out that I haven't tried this myself, and that it probably won't work in the following scenarios:

  • Transparent items - generally, transparent backgrounds cause hit testing of a control to pass to the parent
  • Partially occluded items - you can only hit-test one point at a time, so if only part of your child control is visible you will have to check the correct point
茶色山野 2024-11-06 18:02:23

我发现虽然史蒂夫的方法通常有效,但如果您从子控件中间的某个位置获得一个点,它的工作会更可靠。我猜测可能布局舍入的某个地方使得 InputHitTest 检查有些不精确。因此,将他的第一行更改为以下内容,你就成功了:

var childTopLeft = childControl.TranslatePoint(new Point(childControl.RenderSize.Width/2, childControl.RenderSize.Height/2), parentControl);

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:

var childTopLeft = childControl.TranslatePoint(new Point(childControl.RenderSize.Width/2, childControl.RenderSize.Height/2), parentControl);
烟火散人牵绊 2024-11-06 18:02:23

也许 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文