WPF popup staysopen = false still saves the popup when I click on it

my problem is here, I made a list inside the popup and set popup staysopen = false. But every time a popup appears, I need to click something inside the popup (for example, select an item in the list), and then click outside the popup and it will automatically close. If I do not click anything, and even if I click other elements outside the popup, the popup remains. I need the popup to close without requiring me to click on any item inside it. What can I do? Here is the code, there is another reference to the style of this code, but only some color style.

My control is when the user clicks the text box at the top of the popup window, a list appears. If the user does nothing and does not click anywhere outside this element, the popup closes. Thanks.

I can use the following code to do this in silverlight. But it seems that in wpf it no longer works.

popupAncestor = FindHighestAncestor(this.ListBoxPopup); if (popupAncestor == null) { return; } popupAncestor.AddHandler(System.Windows.Controls.Primitives.Popup.MouseLeftButtonDownEvent, (MouseButtonEventHandler)ClosePopup, true);

 <Grid x:Name="MainGrid" Margin="0" VerticalAlignment="Center"> <Grid.RowDefinitions> <RowDefinition Height="20"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> </Grid.RowDefinitions> <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}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="19"></ColumnDefinition> </Grid.ColumnDefinitions> <TextBox x:Name="TextBoxSearchItem" x:FieldModifier="private" HorizontalAlignment="Stretch" Grid.Column="0" VerticalAlignment="Stretch" BorderThickness="0,0,0,0" Background="Transparent" TextChanged="TextBoxSearchItem_TextChanged"></TextBox> <ToggleButton x:Name="DropDownArrorButton" Grid.Column="1" Style="{StaticResource ComboBoxReadonlyToggleButton}"></ToggleButton> <!--<TextBlock HorizontalAlignment="Center" Text="Search" Grid.ColumnSpan="2" TextBlock.FontStyle="Italic" Opacity="0.4" VerticalAlignment="Center"/>--> </Grid> <Grid Grid.Row="1" HorizontalAlignment="Stretch" x:Name="PopupGrid" Margin="0,1,0,0" > <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}"> <ListBox x:Name="ListBoxContainer" Width="{Binding ElementName=MainGrid, Path=ActualWidth}" HorizontalContentAlignment="Stretch" SelectionMode="Single" Height="200" Margin="0" SelectionChanged="ListBoxContainer_SelectionChanged" MouseDoubleClick="ListBoxContainer_MouseDoubleClick"> <ListBox.ItemTemplate> <DataTemplate> <Grid HorizontalAlignment="Stretch"> <Border BorderBrush="{Binding SearchedBackColor}" BorderThickness="{Binding Indicator}" Width="{Binding ElementName=MainGrid, Path=ActualWidth}"> <TextBlock x:Name="ContentText" Text="{Binding Name}" Margin="1,0,0,0"/> </Border> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Popup> <Border x:Name="listBorder" BorderBrush="{StaticResource COL_BTN}" BorderThickness="0,1,0,0" ></Border> </Grid> </Grid> 
+3
source share
1 answer

To control the state of a popup, you must create a dependency property in your view model or a control for "IsPopupOpen", as shown below. You can then bind ToggleButton "IsChecked" and the "IsOpen" popup to this DP.

Also on your ToggleButton set "Focusable = false" and "IsThreeState = false"

  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)); 

Good luck

0
source

Source: https://habr.com/ru/post/1302366/


All Articles