我的要求:我想为我的WPF按钮设置自定义命令,在自定义命令执行中我想知道是通过鼠标单击还是双击执行命令。此外,当customCommand的CanExecute返回false时,我希望按钮继续处于禁用状态。请参阅下面的详细信息。
描述: 嗨,在WPF中,我为我的按钮设置了自定义命令。当我单击按钮(单击并双击)时,命令将被执行。在自定义命令中,我想处理单击和双击的单独操作。是否有可能找到单击或单击内部命令的whetehr按钮?我使用.Net 4.0,c#4.0
注意:我提到了这个How to bind a command in WPF to a double click event handler of a control?,但我在这里面临一个限制 局限性: 当我为我的按钮设置自定义命令然后在CustomCommand CanExcute返回false时按钮进入禁用状态。但是根据上面的建议,通过将命令设置为鼠标绑定并将鼠标绑定设置为按钮可以工作,但是当CanExecute返回false时,该按钮不会进入禁用状态。如何克服这个
public CustomCommand:ICommandd { public bool CanExecute(对象参数) { //任意逻辑 }
public void Execute(object parameter)
{
if(MouseSingleClick)
{
perform ActionA;
}
if(MouseDoubleClick)
{
PerformActionB;
}
}
}
先谢谢。
答案 0 :(得分:1)
我能够使用this并调整它以MVVM友好的方式使用它。
我使用Cinch框架提供了一个工作示例。
我这有助于你提出要开始的想法。
MyViewModel
public class MyViewModel : INotifyPropertyChanged
{
private static DispatcherTimer myClickWaitTimer =
new DispatcherTimer (
new TimeSpan (0, 0, 0, 0, 150),
DispatcherPriority.Background,
mouseWaitTimer_Tick,
Dispatcher.CurrentDispatcher);
private static void mouseWaitTimer_Tick (object sender, EventArgs e)
{
myClickWaitTimer.Stop ();
Debug.WriteLine ("Single Click Executed");//PerformActionA
}
public ICommand CinchSingleClickCommand { get; private set; }
public ICommand CinchDoubleClickCommand { get; private set; }
public MyViewModel ()
{
CinchSingleClickCommand = new SimpleCommand<object, EventToCommandArgs> (CanExecuteSingleCinch, ExecuteSingleCinch);
CinchDoubleClickCommand = new SimpleCommand<object, EventToCommandArgs> (CanExecuteDoubleCinch, ExecuteDoubleCinch);
myClickWaitTimer.Stop ();
}
private void ExecuteDoubleCinch (EventToCommandArgs obj)
{
if (obj.EventArgs is MouseEventArgs)
{
myClickWaitTimer.Stop ();
Debug.WriteLine ("Double Click Executed");//PerformActionB
var mouseEvent = obj.EventArgs as MouseEventArgs;
mouseEvent.Handled = true;
}
}
private bool CanExecuteDoubleCinch (object arg)
{
return true;
}
private void ExecuteSingleCinch (EventToCommandArgs obj)
{
if (!(obj.EventArgs is MouseEventArgs))
{
myClickWaitTimer.Start ();
var mouseEvent = obj.EventArgs as RoutedEventArgs;
mouseEvent.Handled = true;
}
}
private bool CanExecuteSingleCinch (object arg)
{
return true;
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged (string propertyName)
{
var pc = PropertyChanged;
if (pc != null)
pc (this, new PropertyChangedEventArgs (propertyName));
}
}
您可以使用TimeSpan构造函数来设置您希望在单击和双击之间保持多少延迟。
视图
<Window x:Class="DataGridTesting.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:cinch="clr-namespace:Cinch;assembly=Cinch.WPF"
Title="MainWindow"
Height="350"
Width="525">
<DockPanel>
<Button x:Name="button"
Content="Test">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<cinch:EventToCommandTrigger Command="{Binding CinchDoubleClickCommand}" />
</i:EventTrigger>
<i:EventTrigger EventName="Click">
<cinch:EventToCommandTrigger Command="{Binding CinchSingleClickCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</DockPanel>
</Window>
视图背后的代码
public partial class MainWindow : Window
{
public MainWindow ()
{
InitializeComponent ();
this.DataContext = new MyViewModel ();
}
}
我使用Nuget Package Manager为Cinch
,System.Windows.Interactivity
和Microsoft.Expression.Interactions
答案 1 :(得分:0)
混淆是Button控件的默认点击事件阴影超过其他事件,如双击,鼠标按下等。所以可能使用Label是一个好主意。您可以将其伪装成按钮,然后使用标签的MouseDown和MouseDoubleClick事件执行两项不同的任务。使用计时器区分单击。以下链接显示更多详细信息