我的要求:我想为WPF按钮设置自定义命令,在自定义命令执行过程中,我想知道该命令是通过鼠标单击还是双击执行。另外,当customCommand的CanExecute返回false时,我希望按钮进入禁用状态。请参阅下面的详细信息。
说明:嗨,在WPF中,我为按钮设置了自定义命令。当我单击按钮时(对于单击和双击),命令都将执行。在自定义命令中,我要为单击和双击处理单独的操作。是否有可能单击鼠标中的whetehr按钮或在此内部命令中双击它?我使用.Net 4.0,C#4.0
注意:我提到了如何将WPF中的命令绑定到控件的双击事件处理程序?但是我在这里遇到了限制。
限制:当我为按钮设置自定义命令时,然后在CustomCommand CanExcute上返回false时按钮将进入禁用状态。但是根据上述建议,通过将命令设置为鼠标绑定并将鼠标绑定设置为按钮可以使用,但是当CanExecute返回false时,按钮不会变为禁用状态。如何克服这个
public CustomCommand:ICommandd {public bool CanExecute(object parameter){//任意逻辑}
public void Execute(object parameter)
{
if(MouseSingleClick)
{
perform ActionA;
}
if(MouseDoubleClick)
{
PerformActionB;
}
}
}
提前致谢。
我可以使用它,并对其进行调整以以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软件包管理器为提取所需的dll Cinch
,System.Windows.Interactivity
并且Microsoft.Expression.Interactions
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句