wpf 在 MVVM 中绑定事件并将事件参数作为命令参数传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40104765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bind event in MVVM and pass event arguments as command parameter
提问by Visakh V A
I wanted to bind an event with ViewModel.
我想用 ViewModel 绑定一个事件。
I used
我用了
clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity
clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity
and i used trigger for the same
我使用触发器相同
<Canvas Grid.Row="2" Grid.Column="2" x:Name="InteractiveCanvas" Style="{StaticResource canvasChartStyle}" ClipToBounds="True" >
<intr:Interaction.Triggers>
<intr:EventTrigger EventName="MouseEnter">
<intr:InvokeCommandAction Command="AppointmentEditing" />
</intr:EventTrigger>
</intr:Interaction.Triggers>
</Canvas>
but I need event arguments to be used. Here am not able to get the same.
但我需要使用事件参数。这里无法得到相同的结果。
In wpf any possiblity is there to bind event and get event arguments ? With out ussing MVVM lite or PRISM.
在 wpf 中,有没有可能绑定事件并获取事件参数?不使用 MVVM lite 或 PRISM。
I just want to get the event arguments
我只想获取事件参数
回答by Ankit
You can do it by adding the DLL's:
您可以通过添加 DLL 来实现:
System.Windows.InteractivitiyMicrosoft.Expression.Interactions
System.Windows.InteractivitiyMicrosoft.Expression.Interactions
In your XAML:
在您的 XAML 中:
Use the CallMethodAction class.
使用 CallMethodAction 类。
Use the EventNameto call the event you want; then specify your Methodname in the MethodName.
使用EventName来调用您想要的事件;然后Method在MethodName.
<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>
In your ViewModel Code:
在您的 ViewModel 代码中:
public void ShowCustomer()
//The method must be public & can take 0 parameters or 2 parameters i.e.
//object sender & EventArgs args
{
// Do something.
}
P.S: This is a real late response to the question but I hope it helps you.
PS:这是对问题的真正迟到的回答,但我希望它可以帮助您。
回答by Ankit
Should using CommandParameter ..It's all :)
应该使用 CommandParameter ..这一切:)
<Canvas Grid.Row="2" Grid.Column="2" x:Name="InteractiveCanvas" Style="{StaticResource canvasChartStyle}" ClipToBounds="True" >
<intr:Interaction.Triggers>
<intr:EventTrigger EventName="MouseEnter">
<intr:InvokeCommandAction Command="{Binding AppointmentEditing}" CommandParameter="YourParameters" />
</intr:EventTrigger>
</intr:Interaction.Triggers>
</Canvas>
回答by Peregrine
Take a look at the MVVM-Light framework. Their implementation of EventToCommand includes a PassEventArgsToCommand option.
看一看 MVVM-Light 框架。它们的 EventToCommand 实现包括 PassEventArgsToCommand 选项。
See this question, and this old blog postfrom Laurent Bugnion for more details.

