C# 当光标离开窗口但仍停留在主窗口上时,WPF Window MouseLeave事件不会触发
我通过C# 当光标离开窗口但仍停留在主窗口上时,WPF Window MouseLeave事件不会触发,c#,wpf,mouseevent,C#,Wpf,Mouseevent,我通过ShowDialog命令显示了一个窗口,当鼠标离开该窗口时,我想关闭该窗口。但是,当光标离开以绿色突出显示的任何边(见下文)时,MouseLeave事件不会触发,窗口也不会关闭。如果光标离开其中一条以红色突出显示的边,将触发“MouseLeave”事件并关闭窗口 窗口定义: <Window x:Class="OutputDeviceView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentati
ShowDialog
命令显示了一个窗口,当鼠标离开该窗口时,我想关闭该窗口。但是,当光标离开以绿色突出显示的任何边(见下文)时,MouseLeave
事件不会触发,窗口也不会关闭。如果光标离开其中一条以红色突出显示的边,将触发“MouseLeave”事件并关闭窗口
窗口定义:
<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">
为什么在所有情况下都不会触发MouseLeave
事件
它与窗口的显示方式有关。此窗口通过
ShowDialog
方法显示,而其他按预期运行的窗口则使用Show
方法显示。(感谢for)问题似乎在于ResizeMode=“NoResize”
和WindowStyle=“None”
的组合。你考虑过使用弹出窗口吗?@Clemens-直到我开始调查这个问题,我才开始考虑,但我现在正在考虑这个问题。网格或边框对我来说没有什么区别。完全无法解释。这当然是另外一回事。这种行为只是因为模式窗口(ShowDialog),在这种情况下使用a和a没有区别。您可以使用Show而不是ShowDialog来测试它。发现了两个其他相关问题:ResizeMode=“NoResize”
和WindowStyle=“None”
的组合似乎是问题所在。你考虑过使用弹出窗口吗?@Clemens-直到我开始调查这个问题,我才开始考虑,但我现在正在考虑这个问题。网格或边框对我来说没有什么区别。完全无法解释。这当然是另外一回事。这种行为只是因为模式窗口(ShowDialog),在这种情况下使用a和a没有区别。您可以使用Show而不是ShowDialog来测试它。发现了几个其他相关问题:和
/// <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();
}
var outputDevices = new OutputDeviceView(this, _viewModel.OutputDevices, _viewModel.OutputDevice, PointToScreen(Mouse.GetPosition(this)));
if (outputDevices.ShowDialog() == true)
{
_viewModel.OutputDevice = outputDevices.SelectedDevice;
}