鼠标离开时关闭 wpf 弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12317187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:15:18  来源:igfitidea点击:

Closing wpf popup when mouse leaves

c#wpfevents

提问by user589195

I have a label whose mouseover event opens up a popup.

我有一个标签,其鼠标悬停事件会打开一个弹出窗口。

I am trying to handle the mouseleave event on the popup window and close the popup down.

我正在尝试处理弹出窗口上的 mouseleave 事件并关闭弹出窗口。

The problem I'm having is that the mouseleave event is not getting fired until I click anywhere outside the popup window.

我遇到的问题是 mouseleave 事件不会被触发,直到我点击弹出窗口外的任何地方。

Could someone advise me as to what Im doing wrong?

有人可以告诉我我做错了什么吗?

Heres the code.

这是代码。

XAML:

XAML:

<Popup Name="myPopup" IsOpen="False" PlacementTarget="{Binding ElementName=myButton}" StaysOpen="False" MouseLeave="myPopup_MouseLeave">

    <DataGrid MinHeight="400" MinWidth="300" Name="dtgPopup" AutoGenerateColumns="False" ItemsSource="{Binding}" SelectionChanged="dtgPopup_SelectionChanged"  IsReadOnly="True" CanUserAddRows="False">

    </DataGrid>

</Popup>

<Label Name="recentPanels" Content="Recent Panels" MouseEnter="recentPanels_MouseEnter"/>

Event Handlers:

事件处理程序:

private void recentPanels_MouseEnter(object sender, MouseEventArgs e)
        {
            myPopup.IsOpen = true;
        }

        private void myPopup_MouseLeave(object sender, MouseEventArgs e)
        {
            myPopup.IsOpen = false;
        }

采纳答案by Nomad101

From my experience it seems to need the mouse click to realize that the mouse pointer has actually left the form or popup. A work around that is simple to implement is as follows, instead of using the MouseLeave event use the OnMouseLeave.

根据我的经验,似乎需要单击鼠标才能意识到鼠标指针实际上已经离开了表单或弹出窗口。一种易于实现的变通方法如下,而不是使用 MouseLeave 事件,而是使用 OnMouseLeave。

protected virtual void OnMouseLeave(MouseEventArgs e)
{
    myPopup.IsOpen = false;
}

Some more information: http://msdn.microsoft.com/en-us/library/system.windows.controls.control.onmouseleave(v=vs.95).aspx

更多信息:http: //msdn.microsoft.com/en-us/library/system.windows.controls.control.onmouseleave(v=vs.95).aspx

回答by Amadeus Hein

You could handle this solely in wpf if you wish, depending on how clean you want to keep you code behind. This way you can use IsMouseOver instead of detecting MouseLeave/MouseEnter.

如果您愿意,您可以仅在 wpf 中处理此问题,具体取决于您希望将代码保留的干净程度。这样您就可以使用 IsMouseOver 而不是检测 MouseLeave/MouseEnter。

Use a MultiDataTrigger in the TextBlock triggers resources like this:

在 TextBlock 中使用 MultiDataTrigger 触发资源,如下所示:

<MultiDataTrigger>
  <MultiDataTrigger.Conditions>
    <Condition
      Binding="{Binding Mode=OneWay, RelativeSource={RelativeSource Self}, Path=IsMouseOver}" 
      Value="True"
  />
  </MultiDataTrigger.Conditions>
  <Setter
    TargetName="myPopup" 
    Property="IsOpen" 
    Value="True" 
  />
</MultiDataTrigger>