none
WPF Popup doesn't close automatically

    Question

  • Hi,

    I've a Popup with which opens/closes by toggling a ToggleButton. The StaysOpen property for the Popup is set to 'False'. I want the Popup to be closed when I click somewhere else (focusable or non-focusable) other than the ToggleButton. But with the following xaml it's not closing and as a result I can only close the Popup by toggling the button. Can anyone help me out on this. -

    <Grid>
                  <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                  </Grid.ColumnDefinitions>
                  <Microsoft_Windows_Themes:ListBoxChrome BorderThickness="0" ClipToBounds="True" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" MinHeight="{TemplateBinding MinHeight}" Height="{TemplateBinding Height}" Margin="{TemplateBinding Padding}">
                      <ScrollViewer x:Name="PART_ContentHost" Margin="1,1,1,1" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" CanContentScroll="True" />
                  </Microsoft_Windows_Themes:ListBoxChrome>
                  <ToggleButton x:Name="_toggleButton" Grid.Column="1" IsThreeState="False" Style="{StaticResource ComboBoxToggleButton}" Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type afb:AutoFillBox}}, Path=IsComboBoxAutoFill, Mode=OneWay, Converter={StaticResource boolToVisibility}}" />
                  <Popup x:Name="PART_Popup" AllowsTransparency="true" Placement="Bottom" IsOpen="{Binding IsChecked, ElementName=_toggleButton, Mode=TwoWay}" StaysOpen="False" PopupAnimation="{StaticResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}">
                    <Border x:Name="DropDownBorder" Background="{StaticResource {x:Static SystemColors.WindowBrushKey}}" MaxHeight="200" MinWidth="{Binding Path=ActualWidth, ElementName=root}" BorderBrush="{StaticResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1">
                      <ListBox x:Name="PART_ItemList" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" KeyboardNavigation.DirectionalNavigation="Cycle" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling">
                        <ListBox.Resources>
                          <DataTemplate x:Key="DefaultItemTemplate">
                            <StackPanel Orientation="Horizontal" Margin="0,0,2,0">
                              <TextBlock Text="{Binding Path=AutoFillDisplayCode}" FontFamily="Lucida Console" FontSize="12" Margin="0,0,7,0" Foreground="Gray" FontWeight="Light" VerticalAlignment="Bottom" />
                              <TextBlock Text="{Binding Path=AutoFillDisplayText}" VerticalAlignment="Top" />
                            </StackPanel>
                          </DataTemplate>
                        </ListBox.Resources>
                        <ListBox.ItemsPanel>
                          <ItemsPanelTemplate>
                            <VirtualizingStackPanel />
                          </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>
                      </ListBox>
                    </Border>
                  </Popup>
                </Grid>
    Tuesday, May 31, 2011 10:56 PM

Answers

  • Thanks Yves.Z.

    I've already found the solution. The problem was related to the focus on the composition within the Popup and tweaking the focus to the actual control (the RichTextBox extension) when the popup's opens resolved it.


    Somnath
    Friday, June 03, 2011 8:47 PM

All replies

  • I couldnt build your example, there are some references in it that are not included. But i did build a very simple test app that illustrates what you are trying to do. It seems to work ok, maybe you can scale back your implementation to see if something else is preventing it from closing.

     

    <Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
      <Grid>
        <StackPanel>
          <ToggleButton x:Name="btnToggle" Height="50" Width="75">Popup</ToggleButton>
          <ToggleButton x:Name="btnToggle2" Height="50" Width="75">No Popup</ToggleButton>
          <Popup x:Name="popup"
              Width="100" 
              Height="100" 
              IsOpen="{Binding IsChecked, ElementName=btnToggle, Mode=TwoWay}" 
              Placement="MousePoint"
              StaysOpen="False"></Popup>
        </StackPanel>
      </Grid>
    </Window>
    

    Wednesday, June 01, 2011 6:33 AM
  • Hi, in you window grid select mouse leave event. & try this:

    private void Grid_MouseLeave(object sender, MouseEventArgs e)
         { 
           popup.StaysOpen = false;
         }

     

     

     Hope it will help you.


    Regards atik sarker
    • Edited by atik sarker Wednesday, June 01, 2011 9:39 AM For unclear code
    Wednesday, June 01, 2011 9:37 AM
  • Thanks sehlers for your reply. Actually the code I put here is part of the Control Template of an extended RichTextBox. As you suggested I'll try by scaling back the implementation.

    Thanks Again.


    Somnath
    Wednesday, June 01, 2011 3:35 PM
  • Thanks Atik for your reply. As the code I put here is part of a Control Template of a RichTextBox extension, I'll probably try to handle theOnMouseLeave of the extended RichTextBox.

    Thanks and Regards


    Somnath
    Wednesday, June 01, 2011 4:12 PM
  • Hi shyamarcciit,

    If you have any new findings or concerns please feel free to let us know!


    Yves Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 03, 2011 5:44 AM
    Moderator
  • Thanks Yves.Z.

    I've already found the solution. The problem was related to the focus on the composition within the Popup and tweaking the focus to the actual control (the RichTextBox extension) when the popup's opens resolved it.


    Somnath
    Friday, June 03, 2011 8:47 PM
  • Thanks for sharing the experience here!


    Yves Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 09, 2011 8:02 AM
    Moderator