Why does Wpf Popup.IsOpen = true remain open from the ListBox?

I have a window with Button and ListBox.

<Window x:Class="ListBoxFail.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="350" Width="525"> <Grid> <Popup Name="Popup" StaysOpen="False"> <Rectangle Fill="Blue" Width="100" Height="100"/> </Popup> <Button Content="ClickMe" Click="OpenPopup" VerticalAlignment="Top"/> <ListBox SelectionChanged="OpenPopup" VerticalAlignment="Bottom"> <ListBoxItem Content="One"/> <ListBoxItem Content="Two"/> </ListBox> </Grid> </Window> 

The button and ListBox launch a popup window:

  namespace ListBoxFail { partial class MainWindow { public MainWindow() { InitializeComponent(); } private void OpenPopup(object a, object b) { Popup.IsOpen = true; } } } 

When you click the button, a popup window opens. Clicking elsewhere closes the popup as expected.

When you click in the ListBox, a popup window opens. However, the only way to close the popup at this point is to make the main window lose focus. Clicking on a window or pressing a button does not close the pop-up window.

What gives? Why does the ListBox ignore the StaysOpen = false directive? What am I doing wrong?

+5
source share
1 answer

You yourself are not doing anything wrong. ListBox and its focal events can be a bit "iffy". I would suggest looking at how focal events work in a ListBox, but it might be worth considering below:

 <ListView.ItemContainerStyle> <Style TargetType="ContentControl"> <Setter Property="Focusable" Value="False"/> </Style> </ListView.ItemContainerStyle> 

It may be interesting to study, however, if it does not seem to help or solve your problem, let me know and we can take a closer look at this issue.

0
source

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