views:

185

answers:

2

I need to receive some kind of notification 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.

A: 

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.

Graeme Bradbury
I am showing Popup next to TextBox, but Popup itself is inserted int o container above TextBox's Parent (above Border from Original question). Imagine situation where you select item from dropdown and based on that item different content is displayed (some Borders are collapsed, some made Visible). I do not want to place Popup next to TextBox itself since it's automated process to create and show info Popups near text boxes and I don't want to alter Tree of controls (like wrapping TextBox into StackPanel and add Popup to it).
Kestutis
+1  A: 

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;
    }
Kestutis