How to make the Popup control in WPF ignore mouse events

HoWe Yu 41 Reputation points
2024-09-18T06:11:26.24+00:00

I want to implement a function: display Popup when the mouse enters the Grid, update the Popup position when the mouse moves, and close Popup when the mouse leaves the Grid.

This is my current xaml and cs:

MainWindow.xaml

<Window x:Class="ShowPopup.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ShowPopup"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid x:Name="MainGrid" MouseEnter="MainGrid_MouseEnter" MouseMove="MainGrid_MouseMove" MouseLeave="MainGrid_MouseLeave" Background="Transparent">
        <Popup x:Name="MainPopup" IsOpen="True" IsHitTestVisible="False" Placement="Top">
            <Grid Background="Black" Width="200" Height="200"/>
        </Popup>
    </Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

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

    private void MainGrid_MouseMove(object sender, MouseEventArgs e)
    {
        MainPopup.HorizontalOffset = Mouse.GetPosition(MainGrid).X;
        MainPopup.VerticalOffset = Mouse.GetPosition(MainGrid).Y;
    }

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

When the mouse moves from top to bottom, it works fine. But when it moves from bottom to top, Popup will flicker. It should be that when the mouse moves to Popup, MainGrid_MouseLeave is triggered, that is, Popup still receives mouse events. The following is a demonstration picture:Popup

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,002 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hongrui Yu-MSFT 2,465 Reputation points Microsoft Vendor
    2024-09-18T07:06:34.44+00:00

    Hi,@HoWe Yu. Welcome to Microsoft Q&A. 

    To achieve the effect you want, you only need to strictly constrain the execution condition of MainPopup.IsOpen = false;

     

    You could determine whether the mouse has left the Grid area in MainGrid_MouseLeave

    
            private void MainGrid_MouseLeave(object sender, MouseEventArgs e)
    
            {
    
                Point point = Mouse.GetPosition(MainGrid);
    
                if (point.X>MainGrid.Width&&point.Y<MainGrid.Height)
    
                {
    
                    MainPopup.IsOpen = false;
    
                }        
    
            }
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.