C# i MVVM样式中按钮按下和释放的命令
我非常乐意使用C# i MVVM样式中按钮按下和释放的命令,c#,wpf,mvvm,C#,Wpf,Mvvm,我非常乐意使用ICommand实现来处理按钮上的单个操作: <Button Command="{Binding Path=ActionDown}">Press Me</Button> 处理此类需求的正确MVVM方法是什么?您可以轻松创建自己的类,从按钮扩展,为MouseDown和MouseUp定义可绑定的命令 例如: public class PressAndReleaseButton : Button { public static readonly Depe
ICommand
实现来处理按钮上的单个操作:
<Button Command="{Binding Path=ActionDown}">Press Me</Button>
处理此类需求的正确MVVM方法是什么?您可以轻松创建自己的类,从
按钮扩展,为MouseDown和MouseUp定义可绑定的命令
例如:
public class PressAndReleaseButton : Button
{
public static readonly DependencyProperty PressCommandProperty = DependencyProperty.Register(
"PressCommand", typeof(ICommand), typeof(PressAndReleaseButton), new PropertyMetadata(null));
/// <summary>
/// The Press command to bind
/// </summary>
public ICommand PressCommand
{
get { return (ICommand)GetValue(PressCommandProperty); }
set { SetValue(PressCommandProperty, value); }
}
public static readonly DependencyProperty ReleaseCommandProperty = DependencyProperty.Register(
"ReleaseCommand", typeof(ICommand), typeof(PressAndReleaseButton), new PropertyMetadata(null));
/// <summary>
/// The Release command to bind
/// </summary>
public ICommand ReleaseCommand
{
get { return (ICommand)GetValue(ReleaseCommandProperty); }
set { SetValue(ReleaseCommandProperty, value); }
}
/// <summary>
/// Default constructor registers mouse down and up events to fire commands
/// </summary>
public PressAndReleaseButton()
{
MouseDown += (o, a) =>
{
if (PressCommand.CanExecute(null)) PressCommand.Execute(null);
}
MouseUp += (o, a) =>
{
if (ReleaseCommand.CanExecute(null)) ReleaseCommand.Execute(null);
}
}
}
public class按并释放按钮:按钮
{
public static readonly dependencProperty PressCommandProperty=dependencProperty.Register(
“PressCommand”、typeof(ICommand)、typeof(PressAndReleaseButton)、newpropertyMetadata(null));
///
///按下绑定命令
///
公用图标命令,然后按Command
{
获取{return(ICommand)GetValue(PressCommandProperty);}
设置{SetValue(PressCommandProperty,value);}
}
公共静态只读DependencyProperty ReleaseCommandProperty=DependencyProperty.Register(
“ReleaseCommand”、typeof(ICommand)、typeof(press和releasebutton)、newpropertyMetadata(null));
///
///要绑定的释放命令
///
公共ICommand ReleaseCommand
{
获取{return(ICommand)GetValue(ReleaseCommandProperty);}
set{SetValue(ReleaseCommandProperty,value);}
}
///
///默认构造函数注册鼠标向下和向上事件以触发命令
///
公共新闻和新闻按钮()
{
鼠标向下+=(o,a)=>
{
如果(按Command.CanExecute(null))按Command.Execute(null);
}
鼠标悬停+=(o,a)=>
{
if(ReleaseCommand.CanExecute(null))ReleaseCommand.Execute(null);
}
}
}
您可以使用System.Windows.Interactivity
中的EventTrigger
在事件发生时触发命令
<Button Content="Press Me">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding Path=ActionDown}"/>
</i:EventTrigger>
<i:EventTrigger EventName="PreviewMouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding Path=ActionUp}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
我知道你要去哪里,但这不是要绕过RelayCommand
的CanExecute
部分吗?哎呀,我在这方面走得有点太快了。您可以自己调用CanExecute
,让我更新事件句柄。在本例中,我假设Execute的参数中没有参数(因此null
),但您当然可以根据需要使用任何/绑定它们(例如创建PressCommandParameter
依赖属性),除了MouseUp/MouseDown+=
上缺少的逗号之外,用户控件不响应MouseUp/MouseDown
。不过,它们确实会响应PreviewMouseUp/PreviewMouseDown
谢谢,但是您如何将其包装成一个可重用的组件呢?一次过的按钮也可以,但我需要踩下大约20个这样的按钮,所以我也在寻找方法,尽量减少切割和过去。
<Button Content="Press Me">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding Path=ActionDown}"/>
</i:EventTrigger>
<i:EventTrigger EventName="PreviewMouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding Path=ActionUp}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"