简体   繁体   English

C# WPF OnMouseEnter 和 OnMouseLeave 循环

[英]C# WPF OnMouseEnter and OnMouseLeave loops

I have WPF UI Elements that should be hidden when mouse cursor enter in this elements and be visible when mouse cursor leave from this elements.我有 WPF UI 元素,当鼠标光标进入这个元素时应该隐藏,当鼠标光标离开这个元素时应该可见。 For this things i using events OnMouseEnter & OnMouseLeave , like this:为此,我使用事件OnMouseEnterOnMouseLeave ,如下所示:

private void TextBlock_MouseEnter(object sender, MouseEventArgs e)
{
    (e.Source as UIElement).Visibility = Visibility.Hidden;
}

private void TextBlock_MouseLeave(object sender, MouseEventArgs e)
{
    (e.Source as UIElement).Visibility = Visibility.Visible;
}

(code below from https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.panel.zindex?view=netframework-4.8 ) (以下代码来自https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.panel.zindex?view=netframework-4.8

<Canvas>
            <Rectangle Canvas.ZIndex="1" Width="100" Height="100" Canvas.Top="100" Canvas.Left="100" Fill="blue" MouseEnter="TextBlock_MouseEnter" MouseLeave="TextBlock_MouseLeave"/>
            <Rectangle Canvas.ZIndex="2" Width="100" Height="100" Canvas.Top="150" Canvas.Left="150" Fill="yellow" MouseEnter="TextBlock_MouseEnter" MouseLeave="TextBlock_MouseLeave"/>
            <Rectangle Canvas.ZIndex="3" Width="100" Height="100" Canvas.Top="200" Canvas.Left="200" Fill="green" MouseEnter="TextBlock_MouseEnter" MouseLeave="TextBlock_MouseLeave"/>

            <!-- Reverse the order to illustrate z-index property -->

            <Rectangle Canvas.ZIndex="3" Width="100" Height="100" Canvas.Top="300" Canvas.Left="200" Fill="green" MouseEnter="TextBlock_MouseEnter" MouseLeave="TextBlock_MouseLeave"/>
            <Rectangle Canvas.ZIndex="2" Width="100" Height="100" Canvas.Top="350" Canvas.Left="150" Fill="yellow" MouseEnter="TextBlock_MouseEnter" MouseLeave="TextBlock_MouseLeave"/>
            <Rectangle Canvas.ZIndex="1" Width="100" Height="100" Canvas.Top="400" Canvas.Left="100" Fill="blue" MouseEnter="TextBlock_MouseEnter" MouseLeave="TextBlock_MouseLeave"/>
        </Canvas>

But when i run program and hover cursor on the element, it starts to flicker and eat up quite a lot of CPU resources .但是当我运行程序并将光标悬停在元素上时,它开始闪烁并占用大量 CPU 资源

Debugging shows that when i hover cursor on element, events loops until I move the cursor off the element.调试显示,当我将光标悬停在元素上时,事件会循环,直到我将光标移出元素。

I saw this article , but I don't understand the solution that is attached there.我看到了这篇文章,但我不明白那里所附的解决方案。

What do I need to do to prevent these events from looping?我需要做什么来防止这些事件循环?

When you change the Visibility of a UIElement to Hidden, you are actually triggering a MouseLeave event because the mouse hit test is now performed on the element behind it.当您将 UIElement 的 Visibility 更改为 Hidden 时,您实际上是在触发 MouseLeave 事件,因为现在在其后面的元素上执行鼠标命中测试。 And that runs your event handler, which sets the Visibility to Visible which then triggers the MouseEnter event.这将运行您的事件处理程序,它将 Visibility 设置为 Visible,然后触发 MouseEnter 事件。 Hence the flicker.因此闪烁。

One idea to solve this is to use Opacity instead of Visibility.解决这个问题的一个想法是使用 Opacity 而不是 Visibility。 Try:尝试:

private void TextBlock_MouseEnter(object sender, MouseEventArgs e)
{
  (e.Source as UIElement).Opacity = 0;
}

private void TextBlock_MouseLeave(object sender, MouseEventArgs e)
{
  (e.Source as UIElement).Opacity = 1;
}

You are likely causing a "paint event" by repeatedly setting the same value, try this and see if you are happy您可能通过重复设置相同的值导致“绘制事件”,试试这个,看看你是否满意

private void TextBlock_MouseEnter(object sender, MouseEventArgs e)
{
   if(e.Source is UIElement element && element.Visibility != Visibility.Hidden )
   {
     element.Visibility = Visibility.Hidden;
   }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM