我在选项卡(WPF应用程序)中有一个WPF usercontrol(myGraphicControl)。WPF:确定面板是否对用户可见
当表格大小改变时,我重画了myGraphicControl中的图形。
由于重绘操作是我需要做的只有在可见选项卡中的控制。
WPF(用户)控件如何检测它是否“实际可见”?
PS。
由Visible我的意思是说用户可以看到它。 说,如果一个Visible TextBox位于当前不可见的选项卡中,则该文本框不会被用户看到。
我在选项卡(WPF应用程序)中有一个WPF usercontrol(myGraphicControl)。WPF:确定面板是否对用户可见
当表格大小改变时,我重画了myGraphicControl中的图形。
由于重绘操作是我需要做的只有在可见选项卡中的控制。
WPF(用户)控件如何检测它是否“实际可见”?
PS。
由Visible我的意思是说用户可以看到它。 说,如果一个Visible TextBox位于当前不可见的选项卡中,则该文本框不会被用户看到。
也许UIElement.IsVisible会有帮助吗?它适用于标签内容。 无论如何,您可以使用here所述的解决方案。
我还有一个解决方案。 TabControl的当前实现从可视化树中删除不活动的选项卡。因此,另一种确定元素是否可见的方法是找到PresentationSource。对于非活动选项卡的元素,它将为空。
我不相信这里有一个快速修复的解决方案,但您可能可以使用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);
然而一个电话,我要指出,我没有尝试过这个自己,那它可能不会在下列情况下工作:
我发现,虽然史蒂夫的方法一般工作,它会更可靠地工作,如果你从子控件的中间某处得到一个点。我猜测,布局四舍五入可能会使InputHitTest检查有些不准确。所以,将他的第一行改为以下,你就是金:
var childTopLeft = childControl.TranslatePoint(new Point(childControl.RenderSize.Width/2, childControl.RenderSize.Height/2), parentControl);
你是什么意思?在顶部,不以任何方式遮掩? – 2011-04-06 13:11:55
@Erno看到我的编辑。 – serhio 2011-04-06 13:38:15
我有同样的问题,我解决了它。我在这个线程中编写了解决方案:http://stackoverflow.com/questions/1517743/in-wpf-how-can-i-determine-whether-a-control-is-visible-to-the-user/42254899# 42254899 – 2017-02-21 15:43:24