Home > front end >  add MouseButtonEventArgs to userControl in wpf
add MouseButtonEventArgs to userControl in wpf

Time:09-11

I have a usercontrol. This usercontrol has a button that when you click on it, It has to get the mouse position for mainWindow and show a message. the problem is that for getting the mouse position, The event has to be a "MouseButtonEventArgs" event but you can only add "RoutedEventArgs" to user control.

CodePudding user response:

You can get the mouse position relative to your app's MainWindow whenever you want and wihtout using event-handlers at all..

Point mousePosition = Mouse.GetPosition(Application.Currect.MainWindow);

If you want the position relative to some other UIElement

// element can be usercontrol, window dialog, etc..
// it would return negative x,y values if the mouse is out of the element
Point mousePosition = Mouse.GetPosition(element);
  • Related