blaffie
blaffie

Reputation: 505

Mapping TouchMove Events to MouseMove Events in WPF

I'm implementing a simple drag and drop feature in a WPF application. I want this application to run on both desktops with no touch support and also on tablets with only touch support.

Currently I have a MouseMove and TouchMove handler, both implementing the same logic (Starting the DoDragDrop()).

How can I route the input from touch to the mouse handler or visa versa to reduce redundant code? Further how would one route a simple tap to a click event?

Upvotes: 3

Views: 2698

Answers (1)

sa_ddam213
sa_ddam213

Reputation: 43596

I just done a quick test and one way to do this is to create a Global event handler.

Since TouchEventArgs and MouseButtonEventArgs derive from InputEventArgs, your global handler will just implement InputEventArgs

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        //private void Button_TouchMove(object sender, TouchEventArgs e)
        //{
            // TouchEventArgs derives from InputEventArgs
        //}

        // private void Button_MouseMove(object sender, MouseButtonEventArgs e)
        //{
            // MouseButtonEventArgs derives from InputEventArgs
        //}

        private void GlobalHandler(object sender, InputEventArgs e)
        {
            // This will fire for both TouchEventArgs and MouseButtonEventArgs

            // If you need specific information from the event args you can just cast.
            // e.g. var args = e as MouseButtonEventArgs;
        }

    }

Xaml:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid >
        <Button MouseMove="GlobalHandler" TouchMove="GlobalHandler"/>
    </Grid>
</Window>

Hope this helps

Upvotes: 4

Related Questions