2015-07-02 46 views
2

林在我看來,使用Mousebindings聽取用戶的點擊這樣的:WPF:搞清楚一個MouseBinding是否是一個單一的點擊或雙擊

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

我只需要一次鼠標手勢之一。所以,如果我雙擊我的應用程序,我想忽略單擊鼠標左鍵。可能像在初始鼠標點擊後等待1-2秒,然後決定應該調用哪個。有沒有一個簡單的方法來做到這一點?

+0

沒有簡單的方法可用 – Versatile

回答

1

我得到它的工作方式(我用一個按鈕來測試它,你將不得不適應它)。

  1. 使用事件處理程序

    <Button MouseDoubleClick="Button_MouseDoubleClick" Click="Button_Click"></Button> 
    
  2. 店DataContext的一個靜態變量

    private static object context; 
    public MainWindow() 
    { 
        InitializeComponent(); 
        DataContext = new ViewModel(); 
        context = DataContext; 
    } 
    
  3. 適應這個代碼(我主要是得到它從https://stackoverflow.com/a/971676/4792869

    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"); 
    }