0
votes

I have a popup control that appears after a storyboard finishes. This popup is attached to a stackpanel and it is positioned at the bottom.

I have implemented a feature for double click on the stackpanel: When user double click it with mouse left button, the stackpanel slides and disappears.

private void pnlTopMenu_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

For the popup I have set StaysOpen="False" so popup will hide automatically when user clicks wherever on the window outside the popup. This is the default behavior for popup when StaysOpen property is set to false.

My problem here is that the popup behavior (hide on click) is conflicting with the MouseLeftButtonDown event for the stackpanel.

Within MouseLeftButtonDown I check if e.ClickCount >= 2. If so, I slide the stackpanel and hide it. The stackpanel is shown when user click on a button: a storyboard begins and shows it, after storyboard finishes, popup is also shown for the stackpanel.

So it happens the following.

  1. User click on a button so a storyboard begins and it shows the stackpanel and when storyboard finishes it shows the popup attached to the stackpanel at the bottom of the stackpanel.
  2. At this point stackpanel and popup is shown. Now, if user double click to hide the stackpanel, when pnlTopMenu_MouseLeftButtonDown is fired, e.ClickCount >= 2 is not satisfied so panel is not hidden because popup has consumed 1 click to hide itself... so this is a problem for me as stackpanel is not being hidden on double click, so how can I solve this? Is there any way that popup closes without consuming mouse click?
1

1 Answers

0
votes

I have solved it by doing the following.

  1. Setting StaysOpen="True" for Popup control
  2. Within pnlTopMenu_MouseLeftButtonDown I do the following:

    private void pnlTopMenu_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {            
        if (e.ClickCount == 1)
        {
            // I manually close Popup control, so both Popup and stackpanel will be closed on mouse left button down double click
            this.MyPopup.IsOpen = false;
        }
        else if (e.ClickCount >= 2)
        {
            // Story board that starts and hide the stackpanel on double click.
            Storyboard sb = Resources["sbHideTopMenu"] as Storyboard;
            sb.Begin(pnlTopMenu);
        }
    }
    

So if user double clicks wherever on the stackpanel, both popup and stackpanel will be hidden. If user only do a single click wherever on the stackpanel, only popup will be hidden (it is equivalent when StaysOpen="False" is set for Popup) and stackpanel will be kept visible (not hidden).

Additionally, to maintain popup positioned on window respositioning, moving or resized, I have applied below piece of code explained here. That is, in my code-behind (xaml.cs) I do below:

        DependencyObject myPopupPlacementTarget = this.MyPopup.PlacementTarget;
        Popup myPopup = this.MyPopup;

        Window w = Window.GetWindow(myPopupPlacementTarget);
        if (null != w)
        {
            w.LocationChanged += delegate (object sender, EventArgs args)
            {
                var offset = myPopup.HorizontalOffset;
                myPopup.HorizontalOffset = offset + 1;
                myPopup.HorizontalOffset = offset;
            };

            w.SizeChanged += delegate (object sender, SizeChangedEventArgs args)
            {
                var offset = myPopup.HorizontalOffset;
                myPopup.HorizontalOffset = offset + 1;
                myPopup.HorizontalOffset = offset;
            };
        }

where MyPopup is the Popup control in the xaml view.

The only problem is when resizing, when window gets minimized to a little size, sometimes the popup is drawn outside the main window.