Note that there are some explanatory texts on larger screens.

plurals
  1. POClosing a Popup when its parent gets collapsed
    text
    copied!<p>I've been struggling with this for quite some time and I can't seem to find a proper solution. This is the scenario stripped down. Imagine you have the following XAML:</p> <pre><code>&lt;Grid x:Name="LayoutRoot" Background="White"&gt; &lt;Grid x:Name="Host" Width="200" Height="200"&gt; &lt;Popup IsOpen="True"&gt; &lt;Button Content="Some Button" Click="Button_Click" /&gt; &lt;/Popup&gt; &lt;/Grid&gt; &lt;/Grid&gt; </code></pre> <p>In the <strong>Button_Click</strong> event handler all I do is collapse the grid with name <strong>Host</strong>.</p> <pre><code>private void Button_Click(object sender, RoutedEventArgs e) { this.Host.Visibility = System.Windows.Visibility.Collapsed; } </code></pre> <p>What I expected was that the Popup would close therefore hiding the Button. I understand that Popups are not in the same VisualTree and I understand why this might not be working the way I expect it to, but there should be some kind of mechanism for this to happen automatically. The only workaround that comes to my mind is on LayoutUpdated to traverse the visual tree up and ask each Button's parent if it is visible and if I meet a collapsed parent -> close the Popup. However, imagine the performance hit having a HUGE visual tree. It's insane to traverse the visual tree on every layout pass. I'm open to any sort of suggestions.</p> <p><strong>EDIT: It seems that I did not explain fully my scenario. The case is to collapse the Popup if <em>ANY</em> of its parent gets collapsed (not just the immediate one). In WPF there is a useful property called <em>IsVisible</em> which is different than Visibility. For example, <em>Visibility</em> might still be <em>Visible</em>, but <em>IsVisible</em> will be <em>false</em> in this scenario.</strong></p> <p>Best Regards, K</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload