WPF:如何判断鼠标绑定是单击还是双击

3

我正在使用鼠标绑定在我的视图中监听用户点击,就像这样:

<Path.InputBindings>
            <MouseBinding Gesture="LeftDoubleClick" Command="{Binding DoubleLeftClickProj}" />
            <MouseBinding Gesture="LeftClick" Command="{Binding SingleLeftClick}"/>
</Path.InputBindings>

我每次只需要一种鼠标手势。所以,如果我双击应用程序,我想忽略单击左键的鼠标绑定。可能需要在初始鼠标单击后等待1-2秒,然后决定调用哪个手势。有没有简单的方法来实现这个?


没有简单的方法。 - Versatile
1个回答

1
我用以下方法使其工作(我使用了一个按钮进行测试,您需要进行适当的调整)。
  1. Use Event Handlers

    <Button MouseDoubleClick="Button_MouseDoubleClick" Click="Button_Click"></Button>
    
  2. store the DataContext in a static variable

    private static object context;
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
        context = DataContext;
    }
    
  3. Adapt this code (I mainly got it from https://dev59.com/r3NA5IYBdhLWcg3wa9Gp#971676)

    private static DispatcherTimer myClickWaitTimer =
    new DispatcherTimer(
        new TimeSpan(0, 0, 0, 1),
        DispatcherPriority.Background,
        mouseWaitTimer_Tick,
        Dispatcher.CurrentDispatcher);
    
    private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        // Stop the timer from ticking.
        myClickWaitTimer.Stop();
    
        ((ICommand)DataContext).Execute("DoubleLeftClickProj");
        e.Handled = true;
    }
    
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        myClickWaitTimer.Start();
    }
    
    private static void mouseWaitTimer_Tick(object sender, EventArgs e)
    {
        myClickWaitTimer.Stop();
    
        // Handle Single Click Actions
        ((ICommand)context).Execute("SingleLeftClick");
    }
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接