您需要創建一個自定義控件並使用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>
感謝您提供完整的解決方案。只有一件事是我需要能夠使用兩個不同的命令:一個用於點擊,另一個用於保持。 –
我添加了另一個命令「ClickAndHoldCommand」給SmartButton。您應該將您的命令綁定到這個命令並啓用相應的屬性。 – Akanksha
「EnableClickHold」是不需要的,因爲我希望能夠命令指定不同的點擊命令,並保持到同一個按鈕 –