The popup does not lose focus and closes until I clicked on the control inside it.

I am trying to create a drop down control consisting of a ToggleButton and Popup element with a TabControl inside. My problem is that the popup does not close automatically unless I click on a specific control.

Consider the example below, where the popup contains a TabControl, which itself contains the calendar control inside the TabItem.

The expected behavior is that Popup closes whenever it loses focus (that is, by clicking the container window), but in order for the popup to fire the LostFocus event and thus close, I have to click one of the arrow buttons in the calendar, first control.

<UserControl x:Class="DropDownExample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"> <Grid> <ToggleButton x:Name="ToggleButton" ClickMode="Press">Example</ToggleButton> <Popup x:Name="Popup" Placement="Bottom" AllowsTransparency="True" StaysOpen="False" PopupAnimation="Slide" FocusManager.IsFocusScope="false"> <TabControl x:Name="TabControl" MinHeight="200"> <TabItem> <Calendar /> </TabItem> </TabControl> </Popup> </Grid> </UserControl> 

Opening / closing a popup is controlled in checked / unverified ToggleButton events.

+6
source share
2 answers

The problem is that ClickMode = Click. Setting ClickMode = Release fixes the problem and the popup closes when focus is lost.

+8
source

I have no problem closing Popup when I click elsewhere on the screen using this code:

 <Window x:Class="AutomaticPopupClosing.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="100" Width="240"> <Grid> <Button Content="Show Popup" VerticalAlignment="Center" HorizontalAlignment="Center" Click="ButtonBase_OnClick" /> <Popup x:Name="Popup" StaysOpen="False" FocusManager.IsFocusScope="False" PopupAnimation="Slide" AllowsTransparency="True"> <Border Padding="5" Background="White" FocusManager.IsFocusScope="False"> <StackPanel> <TabControl x:Name="TabControl" MinHeight="200"> <TabItem> <Calendar /> </TabItem> </TabControl> </StackPanel> </Border> </Popup> </Grid> </Window> 

In the ButtonBase_OnClick method, I simply set true to the Popup.IsOpen property:

 private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { Popup.IsOpen = true; } 

Do you have anything else worth noting? I could not fix your problem.

Edit : after reading your comments, I tried moving the above code to a user control. The code is basically the same:

 <UserControl x:Class="PopupDoesNotClose.PopupCalendar" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="50" d:DesignWidth="100"> <Grid> <Button Content="Show Popup" VerticalAlignment="Center" HorizontalAlignment="Center" Click="ButtonBase_OnClick" /> <Popup x:Name="Popup" StaysOpen="False" FocusManager.IsFocusScope="False" PopupAnimation="Slide" AllowsTransparency="True"> <Border Padding="5" Background="White"> <StackPanel> <TabControl x:Name="TabControl" MinHeight="200"> <TabItem> <Calendar /> </TabItem> </TabControl> </StackPanel> </Border> </Popup> </Grid> </UserControl> 

MainWindow now looks like this:

 <Window x:Class="PopupDoesNotClose.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:PopupDoesNotClose" Title="MainWindow" Height="100" Width="240"> <local:PopupCalendar /> </Window> 

When I open a popup and try to move the window after using its title bar, the popup closes and I have to click and drag the title bar again to actually perform the move operation. Is there something in your code that is not part of your question? I still cannot recover your problem.

+6
source

All Articles