21

Creating Metro (Microsoft UI) app for Windows 8 on WPF+C#, I met difficulty with PointerPressed event on a button. Event doesn't happen when i perform left-click (by mouse), but it happens in case with right-click or tap. So what's wrong with that event? for example

 <Button x:Name="Somebutton"  Width="100" Height="100"
PointerPressed="Somebutton_PointerPressed"/>
xkillah
  • 641
  • 5
  • 11

4 Answers4

43

The solution is pretty simple: these events have to be handled not through XAML but thorugh AddHandler method.

SomeButton.AddHandler(PointerPressedEvent, 
new PointerEventHandler(SomeButton_PointerPressed), true); 
xkillah
  • 641
  • 5
  • 11
  • 4
    Sorry if this is a stupid question but what is the reason for it not working by declaring it in XAML? – dcdroid Jun 05 '14 at 14:37
  • 7
    @dcdroid Because in XAML there's no way to set bool `handledEventsToo` parameter `true`. But this parameter is important nuance, becuse without it, event is handled somewhere inside Button and "ignores" your handlers – xkillah Jun 06 '14 at 09:57
  • How strange and hard Microsoft world... for simple things – Sergey Orlov Mar 30 '16 at 21:10
  • I'm stuck with the same issue in UWP. Can't find a way to add the event as you suggested. When trying to do this I get an error 'Value does not fall within the expected range'. – Shimmy Weitzhandler Oct 20 '18 at 21:19
  • Thanks a lot, this saved me. I'm working on a UWP project. – AntiqTech Nov 08 '19 at 18:31
4

I have encountered this problem, but was not able to use the accepted answer because my buttons were created dynamically by an ItemsControl, and there was no good place to call AddHandler from.

Instead, I sub-classed Windows.UI.Xaml.Controls.Button:

public sealed class PressAndHoldButton : Button
{
    public event EventHandler PointerPressPreview = delegate { };

    protected override void OnPointerPressed(PointerRoutedEventArgs e)
    {
        PointerPressPreview(this, EventArgs.Empty);
        base.OnPointerPressed(e);
    }
}

Now, the consuming control can bind to PointerPressPreview instead of PointerPressed

<local:PressAndHoldButton
    x:Name="Somebutton"
    Width="100" 
    Height="100"
    PointerPressPreview="Somebutton_PointerPressed"/>

If you want, you can stuff some additional logic in the overridden OnPointerPressed method so that it only fires the event on left-click, or right-click. Whatever you want.

Pete Baughman
  • 2,996
  • 2
  • 19
  • 33
0

FWIW, I've been facing the same issue and solved it by adding the event handler to some other control (but a button).

In my case I had a Button wrapped around a SymbolIcon like so:

<Button PointerPressed="OnTempoPressed" PointerReleased="OnTempoReleased">
  <SymbolIcon Symbol="Add" />
</Button>

What I did I just removed the Button wrap and replaced it with a ViewBox, then added the handlers to the ViewBox itself, everything works now:

<Viewbox PointerPressed="OnTempoPressed" PointerReleased="OnTempoReleased">
    <SymbolIcon Symbol="Add"/>
</Viewbox>

Note that you lose the Button visuals and effects (i.e. hover etc.), but to me that wasn't an issue. I think you can re-apply them from the stock styles.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
-1

If you are you are working with a Button control then try to attach event with "Click" event.

Please note that Button control internally consider and handles PointerPressed, MouseLeftButtonDown, MouseLeftButtonUp and raise Click event. Generally Button control wont allow PointerPressed, MouseLeftButtonDown, MouseLeftButtonUp event to bubble up and fire.

Somnath
  • 3,247
  • 4
  • 28
  • 42
  • 3
    No, i need exactly PointerPressed. Difference described [here](http://stackoverflow.com/questions/13318569/are-click-tapped-and-pointerpressed-synonymous-in-winrt-xaml) – xkillah Feb 08 '13 at 07:17
  • Please note that Button control internally consider and handles PointerPressed, MouseLeftButtonDown, MouseLeftButtonUp and raise Click event. Generally Button control wont allow PointerPressed, MouseLeftButtonDown, MouseLeftButtonUp event to bubble up and fire. – Somnath Feb 08 '13 at 09:07
  • 2
    yes, it was that way for old good desktop windows apps, and for Metro-apps there is no MouseLeftButtonUp, MouseLeftButtonDown, because of another [UIElement class](http://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.ui.xaml.uielement.aspx) – xkillah Feb 09 '13 at 07:19