locked
Can I get the PointerPressed event for Buttons?

    Question

  • I have attached a PointerPressed event to one of my buttons, but the only time I see the event fired is if I press the right mouse button.  Pressing the left mouse button has no effect.  Is it not possible to get these events for Button class controls?

    Thanks.

    Thursday, July 5, 2012 4:49 AM

All replies

  • In general you should handle the Click event for Buttons instead of PointerPressed.

    The Button control handles the PointerPressed event internally to implement its hover/click behavior and marks the PointerPressed routed event as handled - this means the event will not be bubbled up to your handler by default.

    There's an overview of the routed events model and its interaction with controls here, under the "Input Event Handlers in Controls":

    http://msdn.microsoft.com/en-us/library/cc189018(v=VS.95).aspx

    (this documentation is for Silverlight, but the concept is the same)

    If you really need the PointerPressed event for some special reason you can use the AddHandler method to add a handler even though the routed event doesn't normally get raised, but the need for this should be rare.  In general for user interaction you should always use Click.

    Thursday, July 5, 2012 4:51 PM
  • Thanks for the feedback.  What I really want is to be able to set different backgrounds for the button based on it's state.  I have been trying in vain to find a way to do this (why is it so hard?).  I can set the background for the button, but that background is only used for the normal state and the system uses the default backgrounds for the hover and pressed states.  I have posted other messages asking how to accomplish this, but have yet to get a response.  If you can tell me how to do this, I would greatly appreciate it.

    Thanks.

    Friday, July 6, 2012 1:15 AM
  • XAML controls are designed so that you can easily customize or replace any visuals for any state using the VisualStateManager and reusable ControlTemplates.  In Blend or Visual Studio you can right click a Button and select Edit Template > Edit a Copy to create a new template and start editing it.  This is often more easily accomplished in Blend since it has a full visual workflow for editing states.

    Here's an overview topic that should serve as a good starting point:

    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh465374.aspx

    Friday, July 6, 2012 4:47 PM
  • Thanks for the link.  I have already read that article, but I need to know how to control the look of the controls via C++ rather than XAML.  Do you have any examples of how that can be done?

    Thanks.

    Friday, July 6, 2012 11:04 PM
  • Just to clarify - you're building a XAML C++ app (not DirectX), correct?

    Is there a specific reason you're looking to accomplish this purely in code?  In general apps always use XAML markup to define the UI for a C++ Metro app, including control styles.  If needed you can define a template for the Button control in XAML and store it as a resource in the app, then reference that from your C++ code-behind.

    Monday, July 9, 2012 4:42 PM
  • If by XAML you mean a 'Metro' style app, yes, that is what I am building (although I AM using DirectX for drawing).  The reason I need to do this from code, is that we have a number of apps from other platforms (e.g. iOS and Android) that we are porting to Metro.  Rather than rewriting our apps for each platform, we have developed a cross platform tool which allows us to build the app for multiple platforms from one set of source files.  We have a page layout structure much like XAML which defines each page, but we need to build the pages dynamically using our XML source files so that we do not have to create new 'Metro' layouts for each of our apps.  This involves being able to 'skin' the controls such as buttons, hence the desire to accomplish this all in code.

    That being said, rather than using your button controls I have created my own custom control which allows me to completely control the look.  I must say, however, that I am flabbergasted that I needed to do this.  Why didn't you anticipate that someone might want to control the look of ALL of the states of a button and provide for a convenient way to do that?  I mean, if I am going to change the look of the button by setting the 'background' property, what are the chances that I will be happy with the default look for the other states?

    Anyway, thanks for your help.



    • Edited by John Gaby Thursday, July 12, 2012 7:35 PM
    Thursday, July 12, 2012 7:34 PM
  • @Jesse Can you show how to use AddHandler to add the PointerPressed handler to a button? I'm in a situation where I need to use this as my app's telephone keypad buttons require to fire a command at both down and up events.

    Tuesday, September 11, 2012 8:23 PM
  • Sample code:

    button1->AddHandler(UIElement::PointerPressedEvent, ref new PointerEventHandler(this, &MainPage::button1_PointerPressed), true);
    
    ...
    
    void MainPage::button1_PointerPressed(Object^ sender, PointerRoutedEventArgs^ e)
    {
    	
    }

    Or inline:

    button1->AddHandler(UIElement::PointerPressedEvent, ref new PointerEventHandler([](Object^ sender, PointerRoutedEventArgs^ e)
    {
       // ...
    }), true);

     
    Wednesday, September 12, 2012 10:49 PM