Search code examples
c#wpfmouseopenfiledialog

Mouse left button up event and openfiledialog


I have few images in a grid, then when i click a button, a "open file dialog" comes up.(of course, over the images)

Microsoft.Win32.OpenFileDialog dlgOpenFiles = new Microsoft.Win32.OpenFileDialog(); dlgOpenFile.DoModal();

The images have a LeftButtonUp event attached. The problem is that if i select a file by double clicking it, the open file dialog closes(which is good), but besides that, the image behind the clicked file is receiving a LeftButtonUp message which is not good at all.

I am using wpf/c#/vs2010


Solution

  • The simple way to get around it, is whenever you need a handler to button-up event, add a button-down event, do CaptureMouse() in it. Now in your button-up event you can ignore all events, which happen without IsMouseCaptured. And make sure not to forget ReleaseMouseCapture():

    private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        CaptureMouse();
    }
    
    private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (!IsMouseCaptured)
            return;
        ReleaseMouseCapture();
        var dlg = new OpenFileDialog();
        var res = dlg.ShowDialog(this);
        // ...
    }