Ashley Davis
Ashley Davis

Reputation: 10039

In WPF is there a way to determine if another control captures the mouse?

I have a control in my application that needs to know when any other control in the visual tree captures the mouse.

Is this possible?

Upvotes: 1

Views: 554

Answers (1)

DmitryG
DmitryG

Reputation: 17848

Use the Mouse.GotMouseCapture attached event.

public partial class MainWindow : Window {
    public MainWindow() {
        InitializeComponent();
    }
    static MainWindow() {
        EventManager.RegisterClassHandler(typeof(UIElement), Mouse.GotMouseCaptureEvent, new MouseEventHandler(MainWindow_GotMouseCapture));
    }
    static void MainWindow_GotMouseCapture(object sender, MouseEventArgs e) {
        // e.OriginalSource is a captured element
    }
}

Note, that the captured element is available via the Mouse.Captured static property.

Upvotes: 2

Related Questions