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> </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>