2013-12-10 126 views
2

我希望能夠以兩種不同的Command分配到ButtonMVVM:按鈕保持事件的命令

  • Click事件Command
  • Hold事件的命令,它使用HoldTimeout屬性來指定舉行時間

    public static readonly DependencyProperty HoldCommandProperty = 
    DependencyProperty.Register(
         "HoldCommand", 
         typeof(ICommand), 
         typeof(CommandButton), 
         new PropertyMetadata(null, 
          CommandChanged)); 
    
    public ICommand HoldCommand 
    { 
        get { return (ICommand)GetValue(CommandProperty); } 
        set { SetValue(CommandProperty, value); } 
    } 
    

如何計算點擊次數爲&的時間以及計算應該在哪裏完成?如果使用按鈕的「Command」屬性,我不確定處理Click事件是否正確。

結果XAML看起來應該像:

<CommandButton x:Name="InputButton" 
       Command="{Binding PrimaryCommand}" 
       CommandParameter="{Binding}" 
       HoldCommand="{Binding SecondaryCommand}" 
       HoldCommandParameters="{Binding}" 
       HoldTimeout="2000"/> 

我已閱讀如何實現雙擊,但是這不完全是:

回答

3

您需要創建一個自定義控件並使用DispatcherTimer類對其進行計時。您可以添加另一個布爾和命令屬性來激活此行爲。

控制如下:

public class SmartButton : Button 
{ 
    private DispatcherTimer _timer; 


    public int MillisecondsToWait 
    { 
     get { return (int)GetValue(MillisecondsToWaitProperty); } 
     set { SetValue(MillisecondsToWaitProperty, value); } 
    } 

    public DispatcherTimer Timer 
    { 
     get { return _timer; } 
     set { _timer = value; } 
    } 



    public ICommand ClickAndHoldCommand 
    { 
     get { return (ICommand)GetValue(ClickAndHoldCommandProperty); } 
     set { SetValue(ClickAndHoldCommandProperty, value); } 
    } 


    public bool EnableClickHold 
    { 
     get { return (bool)GetValue(EnableClickHoldProperty); } 
     set { SetValue(EnableClickHoldProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for EnableClickHold. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty EnableClickHoldProperty = 
     DependencyProperty.Register("EnableClickHold", typeof(bool), typeof(SmartButton), new PropertyMetadata(false)); 



    // Using a DependencyProperty as the backing store for ClickAndHoldCommand. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty ClickAndHoldCommandProperty = 
     DependencyProperty.Register("ClickAndHoldCommand", typeof(ICommand), typeof(SmartButton), new UIPropertyMetadata(null)); 



    // Using a DependencyProperty as the backing store for MillisecondsToWait. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty MillisecondsToWaitProperty = 
     DependencyProperty.Register("MillisecondsToWait", typeof(int), typeof(SmartButton), new PropertyMetadata(0)); 


    public SmartButton() 
    { 
     this.PreviewMouseLeftButtonUp += OnPreviewMouseLeftButtonUp; 
     this.PreviewMouseLeftButtonDown += OnPreviewMouseLeftButtonDown; 

    } 

    private void OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
    { 
     if (EnableClickHold) 
     { 

       bool isMouseReleaseBeforeHoldTimeout = Timer.IsEnabled; 
       ResetAndRemoveTimer(); 
       // Consider it as a mouse click 
       if (isMouseReleaseBeforeHoldTimeout) 
       { 
        Command.Execute(CommandParameter); 
       } 
       e.Handled = true; 
     } 
    } 

    private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     if (EnableClickHold) 
     { 
      Timer = new DispatcherTimer(DispatcherPriority.Normal, this.Dispatcher) 
      { 
       Interval = TimeSpan.FromMilliseconds(MillisecondsToWait) 
      }; 
      Timer.Tick += Timer_Tick; 
      Timer.IsEnabled = true; 
      Timer.Start(); 
      e.Handled = true; 
     } 
    } 

    void Timer_Tick(object sender, EventArgs e) 
    { 
     this.ClickAndHoldCommand.Execute(this.CommandParameter); 
     ResetAndRemoveTimer(); 
    } 

    private void ResetAndRemoveTimer() 
    { 
     if (Timer == null) return; 
     Timer.Tick -= Timer_Tick; 
     Timer.IsEnabled = false; 
     Timer.Stop(); 
     Timer = null; 
    } 
} 

這樣做的XAML應該像

<wpfMouseClick:SmartButton x:Name="MySmartButton" 
            Width="100" 
            Height="50" 
            ClickAndHoldCommand="{Binding Path=MyTestCommand, 
                   ElementName=MyWindow}" 
            EnableClickHold="True" 
            MillisecondsToWait="1000"> 
      Click and Hold 
     </wpfMouseClick:SmartButton> 
+0

感謝您提供完整的解決方案。只有一件事是我需要能夠使用兩個不同的命令:一個用於點擊,另一個用於保持。 –

+0

我添加了另一個命令「ClickAndHoldCommand」給SmartButton。您應該將您的命令綁定到這個命令並啓用相應的屬性。 – Akanksha

+0

「EnableClickHold」是不需要的,因爲我希望能夠命令指定不同的點擊命令,並保持到同一個按鈕 –

2

查看RepeatButton控件,該控件從單擊它到發佈時反覆觸發一個Click事件。

爲了擴大這一點,您可以控制觸發事件的時間間隔,並記錄在給定時間內將執行多少個事件。例如,如果Interval屬性設置爲,它將每秒觸發一個Click事件。跟蹤有多少人用櫃檯開火;一旦已解僱,這意味着用戶將按鈕按下五秒鐘,並且您可以將事件處理器中的「點擊&保持」事件邏輯放入事件處理程序中,然後重置計數器。

+0

好主意。我想知道如何獲得點擊計數參考如果HoldTimeout設置爲2500毫秒例如? –