开发者

LeftClick firing on mouse down?

开发者 https://www.devze.com 2023-03-29 14:00 出处:网络
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 dow

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 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>
0

精彩评论

暂无评论...
验证码 换一张
取 消