Reputation: 1378
Currently I have to develop a very simple WPF user control that allows the user to select several points on a canvas. The difficulty that I encounter, is that uses with a touch screen should be able to so by triggering a TouchDown event whereas users that don't have touch screen should use the mouse and thus trigger a MouseLeftButtonDown event. Is there a simple way to handle these two cases without duplicating code? Also, I need to use Mvvm Light, so code-behind solutions like How to get Touchscreen to use Mouse Events instead of Touch Events in C# app won't do the trick.
Upvotes: 2
Views: 1120
Reputation: 69959
Your linked question provides an answer for you, whether you are using MVVM or not. Using MVVM does not mean that you cannot handle UI control events. It just means that you should write an Attached Property
to handle them for you. So, your answer is yes, you can handle the two events together and in almost the same way as your linked page suggests.
Your only difference is that the handler must be attached to the events in an Attached Property
. Rather than go over the whole story once again here, I'll just briefly explain the procedure and request that you view my answer from the What's the best way to pass event to ViewModel? question for a code example.
First declare your Attached Property
with its getter and setter and ensure that it has a PropertyChangedCallback
handler attached. The PropertyChangedCallback
handler is where you attach your single handler to the events (the code example just attaches a single event). This just means that it will only attach the handler to the events when your Attached Property
is set. Finally, just add your single handler to handle the two events.
Upvotes: 2