Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF页面上的鼠标事件-仅在子控件上?_C#_Wpf_Mouseevent - Fatal编程技术网

C# WPF页面上的鼠标事件-仅在子控件上?

C# WPF页面上的鼠标事件-仅在子控件上?,c#,wpf,mouseevent,C#,Wpf,Mouseevent,我正在做一个C#WPF项目。主窗口仅包含一个用于导航到不同页面的帧控件 在一个页面上,我使用按钮显示弹出窗口。现在,我想在鼠标离开并移动到页面上方时隐藏此弹出窗口 <Page x:Class="Name.SpaceMainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xml

我正在做一个C#WPF项目。主窗口仅包含一个用于导航到不同页面的
帧控件

在一个页面上,我使用
按钮
显示
弹出窗口
。现在,我想在鼠标离开并移动到
页面上方时隐藏此弹出窗口

<Page x:Class="Name.SpaceMainPage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  Title="MainPage"
  MouseMove="Page_MouseMove">

    <StackPanel>
        <Label Content="Some Label"/>
        <Label Content="Other Label"/>
        <Button Name="PopupButton" Content="Popup" Click="MenuButtonClick" /> 
        <Label Content="Third Label"/>

        <Popup Name="ExtrasPopup" PlacementTarget="{Binding ElementName=PopupButton}" Placement="Top" PopupAnimation="Scroll">
            <StackPanel Background="LightGray">
                ...
            </StackPanel>
        </Popup>
    </Grid>
</Page>


public partial class MainPage : Page {
    ...

    private void MenuButtonClick(object sender, RoutedEventArgs e) {
        ExtrasPopup.IsOpen = true;
    }

    private void Page_MouseMove(object sender, MouseEventArgs e) {
        System.Diagnostics.Debug.WriteLine("Move: " + e.MouseDevice.GetPosition(this));
    }
}

...
公共部分类主页:第页{
...
private void菜单按钮单击(对象发送方,RoutedEventArgs e){
ExtrasPopup.IsOpen=真;
}
私有无效页面\u MouseMove(对象发送方,MouseEventArgs e){
System.Diagnostics.Debug.WriteLine(“Move:+e.MouseDevice.GetPosition(this));
}
}
问题:仅当鼠标移动到子控件(标签或按钮)上方时,才会执行页面移动。如果鼠标移动到页面的空白区域上方,则不会触发事件


对于任何其他鼠标事件(如MouseDown、MouseEnter、MouseLeave等)也是如此。。。这是很奇怪的行为。这是故意的还是我犯了什么错误?

最好注册到按钮上的
MouseLeave
事件或您想要使用的任何控件

<Button Name="PopupButton" Content="Popup" Click="MenuButtonClick" MouseLeave="Menu_MouseLeave" />

试试
@LPL非常感谢!这就是解决办法!单击按钮时会显示弹出窗口,其中还包含一些控件(复选框、按钮等)。因此,鼠标必须离开按钮才能到达弹出窗口内的控件。。。
private void Menu_MouseLeave(object sender, MouseEventArgs e)
{
   ExtrasPopup.IsOpen = false;
}