Skip to main content

Pinch

A gesture recognizer that tracks a pinch gesture. A pinch gesture occurs when two pointer contacts are brought towards each other, or away from each other. This is useful in controls that implement a pinch-to-zoom interaction.

Zoom in

Zoom out

Using a PinchGestureRecognizer​

A PinchGestureRecognizer can be attached to a control using the control's GestureRecognizers property.

<Image Stretch="UniformToFill"
Margin="5"
Name="image"
Source="/image.jpg">
<Image.GestureRecognizers>
<PinchGestureRecognizer/>
</Image.GestureRecognizers>
</Image>
C#
image.GestureRecognizers.Add(new PinchGestureRecognizer());

The PinchGestureRecognizer raises a InputElement.PinchEvent when it detects the start of a pull gesture. When the pull ends, from the pointer being released or another gesture start, it raises a InputElement.PinchEndedEvent. The Scale property in the args passed to the InputElement.PinchEvent event handler contains the relative size of the pinch since it started.

Binding events​

After the PinchGestureRecognizer has been added to your control, you need to bind them in your code behind either through an inline handler or to an event function:

C#
image.AddHandler(InputElement.PinchEvent, (s, e) => { });
image.AddHandler(InputElement.PinchEndedEvent, (s, e) => { });
C#
image.AddHandler(InputElement.PinchEvent, Image_PinchGesture);
image.AddHandler(InputElement.PinchEndedEvent, Image_PinchGestureEnded);
...
private void Image_PinchGesture(object? sender, PinchGestureEventArgs e) { }
private void Image_PinchGestureEnded(object? sender, PinchGestureEndedEventArgs e) { }

If your event handles the gesture completely, you can mark the event as handled by setting:

C#
e.Handled = true;

Pointer type filtering​

PinchGestureRecognizer responds to all pointer types (mouse, touch, and pen). If your application needs pinch-to-zoom only from touch input (for example, to reserve pen input for drawing), you need to create a custom gesture recognizer that filters by PointerType.Touch.

More information​

info

View the source code on GitHub

PinchGestureRecognizer.cs

PinchEventArgs.cs

See also​