c# – Handling stylus events on a ListView

Question:

All touch events trigger corresponding mouse events. So, for Grid, it is enough to subscribe to MouseDown (you can both Right button and Left button), MouseUp, MouseMove, and all stylus events will occur together with mouse events. So there is no point in subscribing to SlylusDown, SlylusUp and StylusMove here.

However, one has only to change the Grid to ListView, and such a convenient thing disappears …

Code that is sufficient to illustrate the problem:

<ListView Background="Transparent" PreviewMouseLeftButtonDown="listView_PreviewMouseLeftButtonDown" PreviewMouseLeftButtonUp="listView_PreviewMouseLeftButtonUp"
          MouseMove="listView_MouseMove"/>


private void listView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
    Debug.WriteLine("DOWN");
}

private void listView_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
    Debug.WriteLine("UP");
}

private void listView_MouseMove(object sender, MouseEventArgs e) {
    Debug.WriteLine("MOVE");
}

Answer:

In general, more precisely, only when StylusMove does not occur MouseMove. Stylus events such as Down and Up still generate corresponding mouse events.

As a solution, I suggest the following simple code:

private void listView_MouseMove(object sender, MouseEventArgs e) {
    HandleMouseMove(sender, e.GetPosition(sender as IInputElement));
}

private void ListView_StylusMove(object sender, StylusEventArgs e) {
    HandleMouseMove(sender, e.GetPosition(sender as IInputElement));
}

private void HandleMouseMove(object sender, Point mousePos) {
    Debug.WriteLine("MOVE: " + mousePos);
}
Scroll to Top