我想用 ViewModel 绑定一个事件。
我用了
clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity
我使用触发器相同
<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>
Run Code Online (Sandbox Code Playgroud)
但我需要使用事件参数。这里无法得到相同的结果。
在 wpf 中,有没有可能绑定事件并获取事件参数?不使用 MVVM lite 或 PRISM。
我只想获取事件参数
您可以通过添加 DLL 来实现:
System.Windows.InteractivitiyMicrosoft.Expression.Interactions在您的 XAML 中:
使用 CallMethodAction 类。
使用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>
Run Code Online (Sandbox Code Playgroud)
在您的 ViewModel 代码中:
public void ShowCustomer()
//The method must be public & can take 0 parameters or 2 parameters i.e.
//object sender & EventArgs args
{
// Do something.
}
Run Code Online (Sandbox Code Playgroud)
PS:这是对问题的真正迟到的回答,但我希望它可以帮助您。