locked
Capturing MouseUp outside of element / window RRS feed

  • Question

  • I have a grid of elements upon which I am performing custom mouse selection. The user MouseDowns on one element, and "drags" around the other elements, selecting a sequence of them via MouseEnters. A MouseUp on the elements ends the selection. It works.

    But, I want the selection to end also if the user MouseUps outside the grid, or even outside the window. Currently, for the both of these scenarios, the mouse goes up and when the cursor is brought back over the grid, the "drag" continues with no mouse button down.

    My first attempt was to dynamically add a PreviewMouseUp to the containing window, which would cancel the selection. This works, but feels a little hacky, and doesn't solve the problem of MouseUps outside the window.

    Please note this is not a DragDrop - I am not moving data around and don't want the drag drop cursors.

    Thoughts?

    Friday, August 13, 2010 4:14 PM

Answers

  • Thanks noorbaksh, although I figured this one out another way.

    On the first MouseDown, I dynamically add a MouseEnter to the grid. This MouseEnter handler checks the state of the Mouse.LeftButton. If it's not pressed, it assumes that it was release somewhere outside the grid and so stops the selection operation. Works well.

    • Marked as answer by rizzlwizzl Friday, August 13, 2010 7:35 PM
    Friday, August 13, 2010 7:35 PM

All replies

  • I use the solution provided here . It works great. For .Net 4.0 usage you have to modify the code a bit (see my thread and answers from others helping out).

     

    Hope this helps.


    noorbakhsh حميد نوربخش
    Friday, August 13, 2010 4:58 PM
  • Thanks noorbaksh, although I figured this one out another way.

    On the first MouseDown, I dynamically add a MouseEnter to the grid. This MouseEnter handler checks the state of the Mouse.LeftButton. If it's not pressed, it assumes that it was release somewhere outside the grid and so stops the selection operation. Works well.

    • Marked as answer by rizzlwizzl Friday, August 13, 2010 7:35 PM
    Friday, August 13, 2010 7:35 PM
  • The other solutions here are extreme overkill for your use case. You can accomplish this with two lines of code, since yours is the very common case when the mousedown occurs inside a control and you want to track subsequent dragging outside the window.

    In your MouseDown handler, add:

    this.CaptureMouse();

    Then, on MouseUp, you will need to release it:

    this.ReleaseMouseCapture();

    Thanks to: https://stackoverflow.com/questions/10330452/mouse-move-not-trigger-outside-wpf-main-window#10338040



    Friday, April 13, 2018 4:35 PM