title: WPF popup staysopen=false still keep the popup open while clicking outside
body: <p>my problem here is I made a listbox inside the popup, and set the popup's staysopen=false. But each time popup box pops, I have to click something inside the popup(like select an element in listbox), then click outside the popup, and it will close automatically. If I don't click anything, and even if I click other elements outside the popup, the popup stays on. I need the popup closes without needing me to click any element inside it. What can I do? Here is the code, there are some other style link to this code but just some color style.</p>
  
  <p>My control is when user click the textbox on the top of the popup box, the listbox pops. If user does nothing and click any place outside this element, the popup box closes. Thanks.</p>
  
  <p>I can use the following code to get it done in silverlight. But seems like in wpf, it is not working anymore.</p>
  
  <p><code>popupAncestor = FindHighestAncestor(this.ListBoxPopup);
              if (popupAncestor == null)
              {
                  return;
              }
              popupAncestor.AddHandler(System.Windows.Controls.Primitives.Popup.MouseLeftButtonDownEvent, (MouseButtonEventHandler)ClosePopup, true);</code></p>
  
  <pre><code>&lt;Grid x:Name="MainGrid" Margin="0" VerticalAlignment="Center"&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="20"&gt;&lt;/RowDefinition&gt;
          &lt;RowDefinition Height="auto"&gt;&lt;/RowDefinition&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;Grid Margin="1,1,1,0" x:Name="TopBar"  Visibility="Visible"  Grid.Row="0" Height="20" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="{StaticResource COL_BTN_LIGHT}"&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="*"&gt;&lt;/ColumnDefinition&gt;
              &lt;ColumnDefinition Width="19"&gt;&lt;/ColumnDefinition&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;TextBox x:Name="TextBoxSearchItem"  x:FieldModifier="private" HorizontalAlignment="Stretch" Grid.Column="0" VerticalAlignment="Stretch" BorderThickness="0,0,0,0" Background="Transparent" TextChanged="TextBoxSearchItem_TextChanged"&gt;&lt;/TextBox&gt;
          &lt;ToggleButton x:Name="DropDownArrorButton" Grid.Column="1" Style="{StaticResource ComboBoxReadonlyToggleButton}"&gt;&lt;/ToggleButton&gt;
          &lt;!--&lt;TextBlock HorizontalAlignment="Center" Text="Search" Grid.ColumnSpan="2" TextBlock.FontStyle="Italic" Opacity="0.4" VerticalAlignment="Center"/&gt;--&gt;
      &lt;/Grid&gt;
      &lt;Grid Grid.Row="1" HorizontalAlignment="Stretch" x:Name="PopupGrid"  Margin="0,1,0,0" &gt;
          &lt;Popup x:Name="ListBoxPopup" StaysOpen="False" x:FieldModifier="private"  IsOpen="{Binding ElementName=DropDownArrorButton, Path=IsChecked, Mode=TwoWay}" 
                 AllowsTransparency="true" Margin="0" HorizontalAlignment="Stretch" Placement="Bottom" 
                 PlacementTarget="{Binding ElementName=TopBar}"  Opened="OnPopupOpened" Closed="OnPopupClosed"
                 HorizontalOffset="{Binding ElementName=PopupGrid, Path=Value, Mode=TwoWay}"
                   VerticalOffset="{Binding ElementName=PopupGrid, Path=Value, Mode=TwoWay}"&gt;
              &lt;ListBox x:Name="ListBoxContainer" Width="{Binding ElementName=MainGrid, Path=ActualWidth}" 
                       HorizontalContentAlignment="Stretch" SelectionMode="Single"  Height="200"  Margin="0" 
                       SelectionChanged="ListBoxContainer_SelectionChanged"
                       MouseDoubleClick="ListBoxContainer_MouseDoubleClick"&gt;
                  &lt;ListBox.ItemTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;Grid HorizontalAlignment="Stretch"&gt;
                              &lt;Border BorderBrush="{Binding SearchedBackColor}" BorderThickness="{Binding Indicator}" Width="{Binding ElementName=MainGrid, Path=ActualWidth}"&gt;
                                  &lt;TextBlock x:Name="ContentText" Text="{Binding Name}" Margin="1,0,0,0"/&gt;
                              &lt;/Border&gt;                               
                          &lt;/Grid&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/ListBox.ItemTemplate&gt;
              &lt;/ListBox&gt;
          &lt;/Popup&gt;                
          &lt;Border x:Name="listBorder" BorderBrush="{StaticResource COL_BTN}" BorderThickness="0,1,0,0" &gt;&lt;/Border&gt;     
      &lt;/Grid&gt;                 
  &lt;/Grid&gt;
  </code></pre>