title: WPF Popup hiding problem
body: <p>Suppose you have a <code>ToggleButton</code> for opening a <code>Popup</code>, same behaviour as all known elements as <code>ComboBox</code> etc.</p>
  
  <p>... which is this code:</p>
  
  <pre><code>&lt;ToggleButton x:Name="PART_OpenToggleButton"
      Focusable="False"   
      IsChecked="False"
      Template="{StaticResource MyToggleButton}"&gt; 
      &lt;Grid&gt;                                           
          &lt;Popup x:Name="PART_PopupControl"
                 Style="{StaticResource MyPopupStyle}"
                 StaysOpen="False"
                 VerticalAlignment="Bottom"
                 IsOpen="False"
                 PlacementTarget="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ToggleButton, AncestorLevel=1}}" /&gt;
      &lt;/Grid&gt;
  &lt;/ToggleButton&gt;
  </code></pre>
  
  <p>Then in code behind you work with .<code>IsOpen</code> for <code>Popup</code> and .<code>IsChecked</code> for <code>ToggleButton</code>.
  Everything works, but the problem arrives when you Open the <code>Popup</code> and click outside the borders.
  The <code>Popup</code> will be closed but the <code>ToggleButton</code> <strong>stays checked</strong>.</p>
  
  <p>You cannot set in the <code>PopupOnClosed</code> Handler that <code>ToggleButton.IsChecked = false</code>, because when you Click on the <code>ToggleButton</code> to close the <code>Popup</code>, the <code>Popup</code> closes itself, sets <code>ToggleButton.IsChecked = false</code> but at the sime time you clicked on the <code>ToggleButton</code> and it <strong>tries to Open</strong> the <code>Popup</code> again. So you cannot close it.</p>
  
  <p>1st ToggleButtonClick:</p>
  
  <pre><code>-&gt; ToggleButton IsChecked = true
  </code></pre>
  
  <hr>
  
  <p>2nd ToggleButtonClick:</p>
  
  <pre><code>-&gt; ToggleButton IsChecked = false
  -&gt; ToggleButton IsChecked = true
  </code></pre>
  
  <hr>
  
  <p>So if you click on the Toggle Button while Popup being open, it blinks but stays open.</p>
  
  <p>How would you solve this problem, please ?</p>
  
  <p><strong>EDITED:</strong></p>
  
  <p>Try this in a MyWindow.XAML and add the dependency property IsDropDownOpen 
  in the code behind, please:</p>
  
  <pre><code>&lt;Grid&gt;
          &lt;ToggleButton x:Name="PART_OpenToggleButton"
                        Focusable="False"                          
                        Height="20"
                        Width="50"
                        IsChecked="{Binding ElementName=TestWindow, Mode=TwoWay, Path=IsDropDownOpen}"&gt;
              &lt;Grid&gt;
  
                  &lt;Popup x:Name="PART_PopupControl"
                         Width="100"
                         Height="100"
                         StaysOpen="False"
                         Focusable="False"
                         VerticalAlignment="Bottom"
                         IsOpen="{Binding ElementName=TestWindow, Path=IsDropDownOpen}"
                         PlacementTarget="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ToggleButton, AncestorLevel=1}}"&gt;                  
                  &lt;/Popup&gt;
              &lt;/Grid&gt;
          &lt;/ToggleButton&gt;
      &lt;/Grid&gt;
  
  public bool IsDropDownOpen
          {
              get { return (bool)GetValue(IsDropDownOpenProperty); }
              set { SetValue(IsDropDownOpenProperty, value); }
          }        
          public static readonly DependencyProperty IsDropDownOpenProperty =
              DependencyProperty.Register("IsDropDownOpen", typeof(bool), typeof(Window), new UIPropertyMetadata(false));
  </code></pre>