简体   繁体   English

如何检查UIElement在Silverlight中是否可见?

[英]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. 你可以做一些代码来测试VisiblityHitTestVisible元素的属性。

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.

相关问题 Silverlight - 确定屏幕上是否显示UIElement - Silverlight - Determine if a UIElement is visible on screen 如何检查UIElement是否为全景图 - How to check if an UIElement is a Panorama or not 如何在Windows Phone Silverlight中获取UIElement的角度? - How to get the angle of an UIElement in Windows Phone Silverlight? 在Silverlight中删除UIElement的父级 - Remove parent of an UIElement in Silverlight 与ObservableDictionary Silverlight的适当UIElement绑定 - Binding with an appropriate UIElement for ObservableDictionary Silverlight 如何将UIElement添加到List <UIElement> ? - How to add UIElement to List<UIElement>? 如何检查我是否正在删除(退格键、删除或“剪切”)RichTextBox 中的 UIElement? - How to check if I'm deleting (Backspace, Delete or 'Cut') a UIElement in a RichTextBox? 以编程方式强制在Silverlight 3 UIElement(文本框)上进行验证 - Programmatically force validation on Silverlight 3 UIElement (Textbox) 未通过UIElement时,Silverlight中的Canvas Click事件 - Canvas Click events in Silverlight when not over a UIElement Silverlight:将UIElement绑定到ListBoxItems时发生TargetInvocationException - Silverlight: TargetInvocationException Occurred by Bind UIElement To ListBoxItems
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM