有这样做的多种方法,看here:
- 使用WPF工具。最简单的
添加命名空间:
- System.Windows.Interactivitiy
- Microsoft.Expression.Interactions
XAML:
<Window>
xmlns:wi="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions">
<wi:Interaction.Triggers>
<wi:EventTrigger EventName="SelectionChanged">
<ei:CallMethodAction
TargetObject="{Binding}"
MethodName="ShowCustomer"/>
</wi:EventTrigger>
</wi:Interaction.Triggers>
</Window>
代码:
public void ShowCustomer()
{
// Do something.
}
- 使用MVVMLight。最困难的,但最好的做法
安装GalaSoft NuGet包。
获取的命名空间:
- System.Windows.Interactivity
- GalaSoft.MvvmLight.Platform
XAML
<Window>
xmlns:wi="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="http://www.galasoft.ch/mvvmlight">
<wi:Interaction.Triggers>
<wi:EventTrigger EventName="Navigated">
<cmd:EventToCommand Command="{Binding NavigatedEvent}"
PassEventArgsToCommand="True" />
</wi:EventTrigger>
</wi:Interaction.Triggers>
</Window>
代码与代表:Source
你必须得到棱镜MVVM NuGet包这一点。
using Microsoft.Practices.Prism.Commands;
// With params.
public DelegateCommand<string> CommandOne { get; set; }
// Without params.
public DelegateCommand CommandTwo { get; set; }
public MainWindow()
{
InitializeComponent();
// Must initialize the DelegateCommands here.
CommandOne = new DelegateCommand<string>(executeCommandOne);
CommandTwo = new DelegateCommand(executeCommandTwo);
}
private void executeCommandOne(string param)
{
// Do something here.
}
private void executeCommandTwo()
{
// Do something here.
}
代码而不DelegateCommand:Source
using GalaSoft.MvvmLight.CommandWpf
public MainWindow()
{
InitializeComponent();
CommandOne = new RelayCommand<string>(executeCommandOne);
CommandTwo = new RelayCommand(executeCommandTwo);
}
public RelayCommand<string> CommandOne { get; set; }
public RelayCommand CommandTwo { get; set; }
private void executeCommandOne(string param)
{
// Do something here.
}
private void executeCommandTwo()
{
// Do something here.
}
- 使用Telerik EventToCommandBehavior。你必须下载它的NuGet Package。这是一个选项。
XAML:
<i:Interaction.Behaviors>
<telerek:EventToCommandBehavior
Command="{Binding DropCommand}"
Event="Drop"
PassArguments="True" />
</i:Interaction.Behaviors>
代码:
public ActionCommand<DragEventArgs> DropCommand { get; private set; }
this.DropCommand = new ActionCommand<DragEventArgs>(OnDrop);
private void OnDrop(DragEventArgs e)
{
// Do Something
}
是这些股票的.NET?我认为他们是棱镜和telerik。你的第三个例子没有传递任何参数? – Julien 2015-04-11 00:23:10
我很确定他们是Idk如何与Prism合作,我不知道telerik是什么。这些只是我发现堆栈溢出和其他网站的方式,我认为其他开发人员可能会觉得方便,我确实发现它很方便,所以我把它放在一个答案中。 – AzzamAziz 2015-04-11 00:32:58
第三个传递绑定,无论它是从哪里继承。 – AzzamAziz 2015-04-11 00:33:14