C# WPF OnMouseEnter and OnMouseLeave loops

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. And that runs your event handler, which sets the Visibility to Visible which then triggers the MouseEnter event. Hence the flicker.

One idea to solve this is to use Opacity instead of 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;
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top