Silverlight的 - 确定如果是的UIElement屏幕上可见 [英] Silverlight - Determine if a UIElement is visible on screen

查看:266
本文介绍了Silverlight的 - 确定如果是的UIElement屏幕上可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与含有两个选项卡的标签控制应用的一种形式。在其中一个选项卡我有一个的UIElement。当它的鼠标悬停计时器启动,并进行一些功能之后的第二次。



问题是用鼠标悬停并立即切换标签时,比我更需要停止计时器。在标签控件的事件做是不可能的,我(选项卡控件不承认定时器)。我希望能够知道什么时候此UIElement是不可见的屏幕上(切换标签时,Visibility属性仍是可见的)



这是它的样子:

 私人无效element_MouseEnter(对象发件人,MouseEventArgs E)
{
timer.Start()
}

私人无效dt_Tick(对象发件人,EventArgs五)
{
//某些功能
}


解决方案

据我所知,有一个测试,如果该元素是可见或不可见没有可靠的方法。



在Silverlight中,有太多的方法为什么一个元件可以被隐藏(画面以外,通过的ScrollViewer,由另一种元素的重叠模糊,使完全透明的,由着色器效果等扭曲)和取消隐藏(通过逆效应指定Z顺序,指定自定义渲染变换,重叠等)



下面是可能会或可能不适合你的需求两种解决方法:




  1. 如果你正在试图做类似的东西工具提示,添加处理程序MouseLeave事件为您的UIElement。在这种情况下,如果定时器是活动的,停止它。


  2. 另外,dt_Tick处理程序可以检查该选项卡通过检查<$ C $显示内C> TabControl.SelectedIndex 属性,如果选择错了就忽略此事件。




更新:这里的#2一些示例代码(未经测试):

 公共静态的IEnumerable< ; FrameworkElement的> visualParents(此FrameworkElement E)
{
的DependencyObject的obj = E;
,而(真)
{
OBJ = VisualTreeHelper.GetParent(OBJ);
如果(空== OBJ)产量突破;
FrameworkElement的FWE = OBJ为FrameworkElement的;
如果(空= FWE!)收益回报FWE;
}
}

公共静态布尔isOnVisibleTab(FrameworkElement的ELT)
{
TabItem的项目= elt.visualParents()OfType< TabItem的>() .FirstOrDefault();
如果(空==项目)
返回真; //没有找到卡,返回true
返回item.IsSelected; //找到该选项卡,如果卡中选择
}


返回true

I have a form in my application with a tab control containing two tabs. in one of the tabs I have a UIElement. When mouse hover on it a timer is started, and after a second some functionality is performed.

The problem is when hovering with the mouse and immediately switching tab, than I need to stop the timer. doing it in the tab control events is not possible to me (the tab control does not recognize the timer). I want to be able to know when this UIElement is not visible on screen (Visibility property is still Visible when switching tabs).

This is how it looked:

private void element_MouseEnter(object sender, MouseEventArgs e) 
    {            
        timer.Start() 
    }

private void dt_Tick(object sender, EventArgs e) 
    {       
       //Some functionality
    }

解决方案

AFAIK, there is no reliable way to test if the element is visible or not.

In Silverlight, there are too many ways why an element can be hidden (outside of the screen, obscure by ScrollViewer, overlap by another element, make completely transparent, distort by a shader effect, etc.) and unhidden (specify Z-order, specify custom render transform, overlap by inverse effect, etc.)

Here are two workarounds that may or may not fit your requirements:

  1. If you’re trying to do something similar to tooltip, add handler to MouseLeave event for your UIElement. In that event, if the timer is active, stop it.

  2. Alternatively, inside dt_Tick handler you can check which tab is shown by checking the TabControl.SelectedIndex property, if the wrong one is selected just ignore this event.

Update: here's some sample code (untested) for the #2:

public static IEnumerable<FrameworkElement> visualParents( this FrameworkElement e )
{
    DependencyObject obj = e;
    while( true )
    {
        obj = VisualTreeHelper.GetParent( obj );
        if( null == obj ) yield break;
        FrameworkElement fwe = obj as FrameworkElement;
        if( null != fwe ) yield return fwe;
    }
}

public static bool isOnVisibleTab( FrameworkElement elt )
{
    TabItem item = elt.visualParents().OfType<TabItem>().FirstOrDefault();
    if( null == item )
        return true;         // Did not find the tab, return true
    return item.IsSelected;  // Found the tab, return true if the tab is selected
}

这篇关于Silverlight的 - 确定如果是的UIElement屏幕上可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆