[英]How to check if a UIElement is visible in Silverlight?
How can I check if any given UIElement is currently visible on the UI? 如何检查给定的UIElement当前是否在UI上可见?
There is the UIElement.Visibility property, but this is set by the progammer to indicate that the element should be hidden or visible. 有UIElement.Visibility属性,但这是由编程人员设置的,以指示该元素应该隐藏或可见。
I already check if the element is in the VisualTree. 我已经检查了元素是否在VisualTree中。
All this does not help if there is another element on top that overlaps it. 如果顶部还有另一个元素与之重叠,那么所有这些都无济于事。
WPF has a property UIElement.IsVisible that seems to do the job, but this is missing in Silverlight. WPF有一个属性UIElement.IsVisible似乎可以完成工作,但是Silverlight中缺少此属性。
Any ideas? 有任何想法吗?
Thanks 谢谢
You can do some code to test the Visiblity
and the HitTestVisible
property of an element. 你可以做一些代码来测试
Visiblity
和HitTestVisible
元素的属性。
EDIT: 编辑:
Try to do something like mentioned here, Silverlight - Determine if a UIElement is visible on screen
尝试执行此处提到的
Silverlight - Determine if a UIElement is visible on screen
There is a way to check the "render visibility" (although it cannot check for overlapping elements): 有一种方法可以检查“渲染可见性”(尽管它无法检查重叠的元素):
Elements do have the Unloaded
event. 元素确实具有
Unloaded
事件。 It is raised whenever changes to the VisualTree happen that result in the Element being part of a VisualTree branch that is not currently rendered. 每当对VisualTree进行更改导致该Element成为当前未渲染的VisualTree分支的一部分时,都会引发此事件。 But luckily you don't have to listen for those events because only loaded elements do have decendants or a parent in the VisualTree.
但幸运的是,您不必侦听这些事件,因为只有加载的元素在VisualTree中确实具有后代或父级。 And with the help of a nice little
ExtensionMethod
you can check whether an element is loaded or not at any given point in time: 借助漂亮的
ExtensionMethod
您可以检查在任何给定时间点是否加载了元素:
public static class FrameworkElementExtensions
{
public static bool IsLoaded(this FrameworkElement element)
{
return element.GetVisualChildren().Any();
//I'm not sure if this alternative is better:
//return System.Windows.Media.VisualTreeHelper.GetParent(element)!= null;
//or
//return element.GetVisualParent() != null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.