Firing MouseLeftButtonDown event programmatically
You can spoof mouse and key events using Win32 interop. Investigate the SendInput function on MSDN/pinvoke.net.
Note that this will cause the system and other applications to think the mouse was actually clicked. If you just want to initiate a WPF event, try RaiseEvent( new RoutedEventArgs( UIElement.MouseLeftButtonDownEvent ) )
.
The wish to trigger a certain event in a control is quite often an indicator of a design problem in the code. Event handlers should trigger behavior, not perform it. I would suggest that you move the code that performs the action triggered by the MouseLeftButtonDown
event handler into a separate method. Then the same method can be called from the ContactDown
event handler.
var grid = new Grid();
int timestamp = new TimeSpan(DateTime.Now.Ticks).Milliseconds;
const MouseButton mouseButton = MouseButton.Left;
var mouseDownEvent =
new MouseButtonEventArgs(Mouse.PrimaryDevice, timestamp, mouseButton) {
RoutedEvent = UIElement.MouseLeftButtonDownEvent,
Source = grid,
};
This is how I fire the event in my test code.