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).
- Is this the correct way to bind to a left click?
- 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 bind开发者_开发知识库ing
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");
}
}
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>
精彩评论