0

I am binding a left click gesture to a WPF button, expecting that it only fires when the mouse is clicked (MouseDown+MouseUp). However, it appears to fire immediately upon pressing the mouse button down (without releasing).

  1. Is this the correct way to bind to a left click?
  2. How do I differentiate between a click and a press in the event handler?

Sample code:

public partial class WpfTest : UserControl
{
    // Gesture for clicking
    public static MouseGesture MouseClickGesture = new MouseGesture(MouseAction.LeftClick);

    // Logon command/gesture binding
    public static RoutedUICommand LogonCommand = new RoutedUICommand();
    public static MouseBinding LogonClickBinding = new MouseBinding(LogonCommand, MouseClickGesture);


    public WpfTest()
    {
        InitializeComponent();

        CommandBindings.Add(new CommandBinding(LogonCommand, LogonClicked));
        Logon.InputBindings.Add(LogonClickBinding);
    }

    private void LogonClicked(object sender, ExecutedRoutedEventArgs e)
    {
        MessageBox.Show("LogonClicked");
    }
}
H.B.
  • 166,899
  • 29
  • 327
  • 400
Travis
  • 2,654
  • 4
  • 26
  • 46

1 Answers1

2

I would not use those bindings, for all i know you can not get a proper click from them (seriously, who designed this?). From what i've seen it is suprisingly hard to get an actual click on an arbitrary control. I would suggest you wrap a Button around whatever you want to be clickable and use the Button's Command /Click-Event.

Change the Button's template to this to make it invisible:

<ControlTemplate TargetType="{x:Type Button}">
    <ContentPresenter/>
</ControlTemplate>
Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • I am using command bindings because I need to dynamically create some buttons at runtime, and I thought that this was the recommended way to do this kind of thing in WPF. When creating buttons at runtime, I guess that you are suggesting I programmatically assign the Click event? – Travis Aug 19 '11 at 16:46
  • @Travis: You can also set the Command if you want to, depends on your use-case. – H.B. Aug 19 '11 at 17:05
  • @Travis: Commands are objects which encapsulate a request, events just contain a list of methods to be called. – H.B. Aug 19 '11 at 19:10