John
John

Reputation: 1051

How to determine if button was clicked or touched?

Is there a way to distinguish whether a button was clicked as in with a mouse or touched using a touchscreen in WPF?

Upvotes: 6

Views: 4789

Answers (3)

ViVi
ViVi

Reputation: 4464

Windows 7 and its higher versions have the ability to receive input from multiple touch-sensitive devices. WPF applications can also handle touch input as other input, such as the mouse or keyboard, by raising events when a touch occurs.

WPF exposes two types of events when a touch occurs − touch events and manipulation events. Touch events provide raw data about each finger on a touchscreen and its movement. Manipulation events interpret the input as certain actions. Both types of events are discussed in this section.

WPF enables applications to respond to touch. For example, you can interact with an application by using one or more fingers on a touch-sensitive device, such as a touchscreen This walkthrough creates an application that enables the user to move, resize, or rotate a single object by using touch.

Source MSDN : https://msdn.microsoft.com/en-us/library/ee649090.aspx

Also read this codeproject article - http://www.codeproject.com/Articles/692286/WPF-and-multi-touch

Upvotes: 1

Derrick Moeller
Derrick Moeller

Reputation: 4950

You can subscribe to PreviewMouseDown and PreviewTouchDown.

Page.xaml

<Button PreviewMouseDown="Button_PreviewMouseDown"
        PreviewTouchDown="Button_PreviewTouchDown" />

Page.xaml.cs

    private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show("Mouse was used.");
    }

    private void Button_PreviewTouchDown(object sender, TouchEventArgs e)
    {
        MessageBox.Show("Touchscreen was used.");
    }

I don't believe you'll be able to access the eventargs of either in the actual click event.

If you need to perform work there as opposed to the preview events I would recommend setting an instance variable in the preview events so when you get to the click event you know where you came from.

Upvotes: 6

GreatJobBob
GreatJobBob

Reputation: 271

You have to set up an event handler. In the designer, double click on the button and that will set on up for you.

Then in the code behind add what ever code you want.

private void Button_Click(object sender, RoutedEventArgs e)
{
    this.Title = "Clicked";
}

You can add Touch events as well TouchDown, TouchUp, etc.

Upvotes: 0

Related Questions