none
Why is my popup not closing when clicking outside?

    Question

  • I have a popup defined that has the StaysOpen property set to False.  The popup is remaining open even when clicked outside of ... would there be any reason for this??

                            <Popup Name="LabelPopup" Height="200" Width="200"  AllowsTransparency="False" IsOpen="False" StaysOpen="False">
                                <Border BorderThickness="1" BorderBrush="Black" Background="White">
                                    <Label HorizontalAlignment="Center" VerticalAlignment="Center" Content="TEST" /> 
                                </Border>
                            </Popup>

    I'm launching the popup from a ToggleButton click event:

            void FillPopup(object sender)
            {
                //EclpImage sourceObject = (EclpImage)sender;
                ToggleButton sourceObject = (ToggleButton)sender;
    
                // Position the popup
                _labelPopup.PlacementTarget = sourceObject;
                //_labelPopup.PlacementRectangle = new Rect(0, 0, 0, 0);
    
                //_labelPopup.Placement = PlacementMode.Relative;
                _labelPopup.PopupAnimation = PopupAnimation.Fade;
                _labelPopup.IsOpen = true;
            }

     

     

    Tuesday, March 23, 2010 12:20 AM

Answers

  • Found out the problem was I was setting focus to a ListBoxItem within the ListBox.

    • Marked as answer by Kofoed Monday, April 05, 2010 5:22 PM
    Monday, April 05, 2010 5:22 PM

All replies

  • I believe Popup doesn't close automatically after you explicitly set Popup.IsOpen to true.  You need to close the popup explicitly when click on outside of popup such as panel. If you could use ToolTip, the ToolTip will close when mouse is away.

    Example:

    <Window x:Class="ToolTipTest2.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <StackPanel MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">
            <ToggleButton Content="ToggleButton" HorizontalAlignment="Left">
                <ToggleButton.ToolTip>
                    <Border BorderThickness="1" BorderBrush="Black" Background="White">
                        <Label HorizontalAlignment="Center" VerticalAlignment="Center" Content="TEST" />
                    </Border>
                </ToggleButton.ToolTip>
            </ToggleButton>
            <Popup Name="LabelPopup" Height="200" Width="200"  AllowsTransparency="False" IsOpen="False" StaysOpen="False">
                <Border BorderThickness="1" BorderBrush="Black" Background="White">
                    <Label HorizontalAlignment="Center" VerticalAlignment="Center" Content="TEST" />
                </Border>
            </Popup>
            <ToggleButton Name="toggleButton" Content="Open Popup" HorizontalAlignment="Left" Click="ToggleButton_Click"/>
        </StackPanel>
    </Window>

    Code:

    using System.Windows;
    using System.Windows.Input;

    namespace ToolTipTest2
    {
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }

            private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                LabelPopup.IsOpen = false;
            }

            private void ToggleButton_Click(object sender, RoutedEventArgs e)
            {
                LabelPopup.PlacementTarget = toggleButton;
                LabelPopup.IsOpen = true;
            }
        }
    }

    Hope it helps.


    William
    Tuesday, March 23, 2010 4:31 AM
  • What's the point of the StaysOpen property then?

    I can't close the popup manually like that because I don't know what's underneath or outside of the popup.  It is a consuming application that my popup has no knowledge of.

    Tuesday, March 23, 2010 6:01 PM
  • Hi,

     

    I believe that the IsOpen property is there for you to manually control the visibility of the popup.

    Once you set it to true, it is your code that will have to manually hide it...

    (Just like the ToolTip behavior)

    Tuesday, March 23, 2010 6:07 PM
  • huh??

    That's the exact same response that William noted above.  I'm making the point that the StaysOpen="False" property should automate this for me.  I should not have to call this IsOpen=False to close the popup.

     

    http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.popup.staysopen.aspx

    Tuesday, March 23, 2010 6:26 PM
  • I made a quick test project and found the StaysOpen property works as expected (after the Popup.IsOpen = True, mouse clicking outside of the Popup causes the Popup to close).

    Do you have any code inside the Popup that can affect the default behavior?

    If you create an empty new project simply to test the Popup.StaysOpen, what behavior will you get?

    Thanks,
    Jie
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback, please tell us.

    The All-In-One Code Framework Project
    My Blog (in Simplified Chinese)
    Thursday, March 25, 2010 9:45 AM
  • Yeah it's strange - I can get a new window popup to work correctly.  I'm not really doing anything special short of constructing it in code and adding a ListBox as it's content.  Very bizarre.
    Thursday, March 25, 2010 10:39 PM
  • Found out the problem was I was setting focus to a ListBoxItem within the ListBox.

    • Marked as answer by Kofoed Monday, April 05, 2010 5:22 PM
    Monday, April 05, 2010 5:22 PM