2013-12-10 5 views
2
I가 두 개의 서로 다른 Command을 할당 할 수 있도록하려는

Button :MVVM : 버튼 홀드 이벤트 명령

  • Click 이벤트 Command
  • 보류 기간을 지정 HoldTimeout 속성을 사용 Hold 이벤트 명령

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

클릭 & 보류 시간을 계산하는 방법과 계산은 어디에서 수행해야합니까? 단추의 '명령'속성을 사용하는 경우 Click 이벤트 처리가 올바른지 잘 모르겠습니다. 내가 더블 클릭을 구현하는 방법을 읽고

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

하지만이 정확히되지 않습니다 : :

결과의 XAML 그렇게 보일 것이다

답변

3

사용자 지정 컨트롤을 만들고 DispatcherTimer 클래스를 사용하여 시간을 조정해야합니다. 다른 부울 및 명령 속성을 추가하여이 동작을 활성화 할 수 있습니다. 이것의 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에 추가했습니다. 이 명령에 명령을 바인드하고 각 특성을 사용 가능하게해야합니다. 개인 무효 OnPreviewMouseLeftButtonUp (개체를 보낸 사람을 MouseButtonEventArgs 전자) { 부울 : – Akanksha

+0

은 'EnableClickHold'내가 다음 편집을해야 발견 같은 버튼 –

2

RepeatButton 컨트롤을 살펴보면 클릭 할 때부터 릴리스 될 때까지 반복적으로 Click 이벤트가 발생합니다.

확장하려면 Click 이벤트의 간격을 제어하고 지정된 시간에 얼마나 많은 이벤트가 실행되는지 추적하십시오. 예를 들어, Interval 속성이 으로 설정된 경우 매 초 Click 이벤트가 발생합니다. 카운터로 해고당한 사람 수를 기록하십시오. once 이 발생했다는 것은 사용자가 5 초 동안 버튼을 누르고있는 것을 의미하며 RepeatButtonClick 이벤트 처리기에 "Click & Hold"이벤트 로직을 넣은 다음 카운터를 재설정 할 수 있습니다.

+0

우수한 생각처럼 보일 것입니다

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

다음과 같이

컨트롤입니다. HoldTimeout이 예를 들어 2500ms로 설정된 경우 클릭 수 참조를 얻는 방법에 대해 궁금합니다. –