none
Custom WPF Button Template

    Question

  • I am building a  WPF Button Template that will overide the base.
    I have two problems.

    First - - -  I change the button appearance triggered on Is Pressed, and have a ButtonDoun and ButtonUp State.  I would like to "hold" the ButtonDown State until the ClickEvent sub has been processed.  Can you think of a way to do this - (In the template).  In other words is there a "ClickComplete" Event, that I dont see?

    Second, The trigger on IsPressed works great for a mouse click, but I would also like to trigger the ButtonDown state if the button has Focus and the Enter Key is pressed as well.  It seems like there must be an event that I can grab, because Focus and EnterKey already causes a click event. As I think about it perhaps I could trigger on the Click event rather than IsPressed and this would then grab the condition I want to show ButtonDown either way.  Of course, I would need the problem stated in the First Part to be solved, in order to return the button to the ButonUp state, perhaps a storyboard here would also provide a minimal ButtonDown period for a nice appearance.

    The result of a successful template is that the screen rendering of buttons would give the user positive feed back about the completion of a sub ( neat when the sub is large, but not long enough for a progress control)  A side benifit would be that this special button would have the equivilant of a singleton pattern,  Also the user would get visual feedback that keyboard navigation took place.

    Thank you in advance.
    Friday, February 19, 2010 3:42 PM

Answers

  • Hi lumberpro,

    -->I found that the OnClick Method cannot be used in XAML. Since I am trying to do this in a template,
    Yes, you are right, we can only execute the event handler at runtime. I think you can utilize the Command property of Button. When we click on the Button, RoutedCommand.Execute method will executed if the CanExecute method return true. Here is a blog post on how to use custom command:
    http://www.switchonthecode.com/tutorials/wpf-tutorial-command-bindings-and-custom-commands

    Thanks.
    Sincerely.
    Jim Zhou -MSFT
    • Marked as answer by lumberpro Tuesday, February 23, 2010 1:24 PM
    Tuesday, February 23, 2010 10:01 AM

All replies

  • You could save yourself some trouble and just use a ToggleButton, unless I am completely misunderstanding what it is you are tryign to do. Just disable it or prevent the IsChecked state from changing until your work is done.

    -Joe


    http://blogs.windowsclient.net/joes
    Friday, February 19, 2010 7:20 PM
  • Joe,

    Thank You for the reply.  I guess I failed to tell the whole story, my button template creates a "Gel button" to compliment an application that I am working on.  So the needs stated above in my original post are kind of "icing on the cake" for my final design.

    In fact I had not considered the ToggleButton as a base, because I was un-aware of it's existence.  I will look at it a bit closer tonight, but on a quick glance, I am afraid it's functionality appears more like a check box than the funtionality I described.  Perhaps, I will learn more as I look closer at it, but so far it does not look like it is likely that it will fill the bill at all, for my needs.

    So I am still looking for some good hints.

    Bob Ranck
    Saturday, February 20, 2010 4:08 AM
  • Hi lumberpro,
    -->I change the button appearance triggered on Is Pressed, and have a ButtonDoun and ButtonUp State. 

    Based on my understanding, you can expose two dependency properties for your custom button class,  something like IsButtonDown and IsButtonUp properties. By overriding the OnClick method,  we set IsButtonDown to true and when OnMouseLeftButtonUp is called, we set IsButtonUp property to true and IsButtonDown to false. Please let me know the status of this thread after you try it.

    Thanks.
    Sincerely.
    Jim Zhou -MSFT
    Saturday, February 20, 2010 12:22 PM
  • Hi, Jim,

    Thanks for your well thought out reply, I have not checked it out yet, but it may work.  I'll have to look around a bit for the "OnClick" in XAML.  The engineer at Microsoft that invents a way to single step in XAML will get my vote for a Gold Medal.

    As I think about it the same approach may fullfill the needs of both the first and second part of my issue.

    I will let you know.

    Bob Ranck
    Saturday, February 20, 2010 1:40 PM
  • Hi Jim,

    Oops! I'm stuck again - After investigating, I found that the OnClick Method cannot be used in XAML. Since I am trying to do this in a template, it appears that it wont work.  I would prefer to exhaust the possibilities in a template before turning to a user control.

    Any other ideas about how to do it in XAML?

    Bob Ranck
    Saturday, February 20, 2010 3:51 PM
  • Hi lumberpro,

    -->I found that the OnClick Method cannot be used in XAML. Since I am trying to do this in a template,
    Yes, you are right, we can only execute the event handler at runtime. I think you can utilize the Command property of Button. When we click on the Button, RoutedCommand.Execute method will executed if the CanExecute method return true. Here is a blog post on how to use custom command:
    http://www.switchonthecode.com/tutorials/wpf-tutorial-command-bindings-and-custom-commands

    Thanks.
    Sincerely.
    Jim Zhou -MSFT
    • Marked as answer by lumberpro Tuesday, February 23, 2010 1:24 PM
    Tuesday, February 23, 2010 10:01 AM
  • Hi Jim,

    You are pretty sharp!  That is exactly what I was looking for.  Thank you so much.

    There is a slight risk that I could create a "feedback loop" but I will try to avoid that.

    The blog also has some intresting other stuff about commanding that I will read.

    Bob Ranck
    Tuesday, February 23, 2010 1:24 PM