How to get "Control no longer visible" notification in Silverlight when any parent is hidden?
I need to receive some kind of notifi开发者_StackOverflow社区cation when control is no longer visible in view. That is if I have control deep in tree (like Border -> Grid -> StackPanel -> TextBox) I need notification on TextBox when Border gets hidden. I DO NOT have access to Border itself, imagine like wrapping control of everything gets collapsed, I still need notificaiton on TextBox that is deep in child controls.
I didn't manage to find any event or property that wouold indicate if Control is visible/not visible when parent is invisible, therefore I had to hook to LayoutUpdated event and check VisibilityProperty of all visual ancestors.
here's snippet if interested:
private bool IsControlVisible(FrameworkElement element)
{
var ancestors = element.GetVisualAncestorsAndSelf().ToList();
foreach(var a in ancestors)
{
Visibility visibility = (Visibility)a.GetValue(FrameworkElement.VisibilityProperty);
if (visibility == Visibility.Collapsed)
return false;
}
return true;
}
UIElements don't notify their children about Visibility changes.
What are you doing that the TextBox needs to respond to Border visibility? Maybe we can help with a solution that doesn't require knowledge of the parents.
精彩评论