C# WPF Tutorial – Mouse Events

C# WPF Tutorial - Mouse Events

WPF Mouse Events are special triggers which occur on special occasions. These events are available on all WPF components, you can also find all possible events from the properties of WPF components. We can also consider these events as input of the mouse.

Each mouse event represents different logical reasoning to prompt and for usage. Each event trigger based on the input of the mouse pointer, buttons and scroll.

These are available WPF Mouse Events.

  • GotMouseCapture
  • LostMouseCapture
  • MouseDown
  • MouseEnter
  • MouseLeave
  • MouseMove
  • MouseUp
  • MouseWheel
  • PreviewMouseDown
  • PreviewMouseDownOutsideCapturedElement
  • PreviewMouseMove
  • PreviewMouseUp
  • PreviewMouseUpOutsideCapturedElement
  • PreviewMouseWheel

There are three ways of defining Mouse events for any WPF component.

First, you can visit the properties of components and double-click on any mouse event to declare it. This method will auto bind the component with the event and write auto code in the C# file. The user does not need to write code at all. A few clicks of the mouse generate these events.

C# WPF Tutorial - Mouse Events - second method

Second, you can write the name of the event on the WPF component and it will ask you to name it instead of naming it you click on “New Event Handler” and then it will do the same as the First method, which means automatically generating C# code.

Third, you can write the name of the event on the WPF component and define the name which you want to give to the event, then you have to write the C# code yourself, only then does it get bind together. Else it will give you an error because WPF does not know where it goes when the event is triggered. So make sure the naming should be solid and the same.

Because events are named sensitive. I added the video for depth concept also below, if you still have any questions you can contact me for further discussion.