1

I have a window shown via the ShowDialog command that I want to close when the mouse leaves that window. However, when the cursor leaves by any of the edges highlighted in green (see below) the MouseLeave event is not fired and the window isn't closed. If the cursor leaves by one of the edges highlighted in red the `MouseLeave event is fired and the window closes.

enter image description here

Window definition:

<Window x:Class="OutputDeviceView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        ResizeMode="NoResize"
        WindowStyle="None"
        ShowInTaskbar="False"
        SizeToContent="WidthAndHeight"
        Background="Gray"
        KeyDown="Window_KeyDown"
        MouseLeave="Window_MouseLeave">

Event handler:

    /// <summary>
    /// Handle the MouseLeave event. Close the window when the mouse leaves the window
    /// </summary>
    /// <param name="sender">The object sending the event</param>
    /// <param name="e">The event arguments</param>
    private void Window_MouseLeave(object sender, MouseEventArgs e)
    {
        e.Handled = true;
        Close();
    }

The code that opens the window:

var outputDevices = new OutputDeviceView(this, _viewModel.OutputDevices, _viewModel.OutputDevice, PointToScreen(Mouse.GetPosition(this)));

if (outputDevices.ShowDialog() == true)
{
    _viewModel.OutputDevice = outputDevices.SelectedDevice;
}

Why isn't the MouseLeave event firing in all cases?

It has something to do with how the window is displayed. This window is displayed via the ShowDialog method, whereas the other windows which behave as expected are displayed using the Show method. (Thanks to Mitra M for pointing that out)

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • 2
    The combination of `ResizeMode="NoResize"` and `WindowStyle="None"` seems to be the problem. Have you considered using a Popup? – Clemens May 30 '20 at 15:26
  • @Clemens - I hadn't until I started investigating this issue, but it's something I'm looking at now. – ChrisF May 30 '20 at 15:37
  • Grid or Border makes no difference for me. And would be totally unexplainable. It's certainly something else. – Clemens May 30 '20 at 18:31
  • This behavior is just because of the modal window (ShowDialog), There is no difference between using a or a in this case. you can test it by using Show instead of ShowDialog. – Maria May 31 '20 at 19:08
  • @MitraM Ah. That is the other difference between the windows. I was too focused on their contents, not the mode of display. – ChrisF May 31 '20 at 21:01
  • 1
    Found a couple other related questions: https://stackoverflow.com/questions/39258393/ismouseover-trigger-doesnt-work-when-using-showdialog-and-borderless-window and https://social.msdn.microsoft.com/Forums/sqlserver/en-US/b1990236-938e-4539-888a-4bfd8ccc3670/button-in-wpf-borderless-dialog-window-doesnt-change-from-mouseover-to-normal-state?forum=wpf – Keith Stein Jun 01 '20 at 02:22

0 Answers0