Silverlight - Determines if a UIElement is displayed on the screen.

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

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

Here's how it looked:

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

private void dt_Tick(object sender, EventArgs e) 
    {       
       //Some functionality
    }
+3
source share
2 answers

AFAIK, , .

Silverlight , ( , ScrollViewer, , , ..) ( Z -order, , ..)

, :

  • - , MouseLeave UIElement. , , .

  • dt_Tick , , TabControl.SelectedIndex, , .

: () # 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
}
+3

Unloaded. , VisualTree, , VisualTree, .

private void element_MouseEnter(object sender, MouseEventArgs e) 
{            
    timer.Start();
    element.Unloaded += OnElementUnloaded;
}

private void OnElementUnloaded(object sender, EventArgs e)
{
    element.Unloaded -= OnElementUnloaded;
    timer.Stop();
}

private void dt_Tick(object sender, EventArgs e) 
{
    element.Unloaded -= OnElementUnloaded;
   //Some functionality
}
0

All Articles