将事件绑定到命令上
在XAML中,InvokeCommandAction类根据MVVM代码后置的范式提供了一种简便的方法将事件“绑定”到ICommand的属性上。
属性
InvokeCommandAction暴露了以下属性
Command标识出调用时要执行的命令。这是必选字段。AutoEnable标识出是否根据命令的CanExecute的结果自动启用或禁用相关元素。这是一个可选字段,默认值是True。CommandParameter标识出要提供给命令的命令参数。这是一个可选字段。TriggerParameterPath标识出事件所提供对象中的路径,该路径将被解析以找到事件所提供对象中相对应的子属性,并用作命令的参数。(csharpshare.com注:该属性可以实现将触发命令的事件所提供的对象本身或子属性传递给ViewModel中的命令对象。)
使用
基本使用
首先需要在WPF中通过指定InteractionTrigger来使用绑定功能。它是WPF中标准的开箱即用功能。通过添加命名空间,以便能够在XAML中声明它。
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
添加Prism命名空间,以便能够在XAML中声明InvokeCommandAction。
xmlns:prism="http://prismlibrary.com"
并将所需的事件附加到控件上。
<Window x:Class="UsingInvokeCommandAction.Views.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/xaml/behaviors"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
Title="{Binding Title}" Height="350" Width="525">
<Grid>
<ListBox ItemsSource="{Binding Items}" SelectionMode="Single">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<prism:InvokeCommandAction Command="{Binding SelectedCommand}"
CommandParameter="{Binding MyParameter}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
</Grid>
</Window>
TriggerParameterPath
在下面的代码中,SelectionChanged事件接收一个SelectionChangedEventArgs对象,该对象有一个IList属性名为AddedItems。使用TriggerParameterPath指定这个属性作为ICommand对象的参数传递。
<Window x:Class="UsingInvokeCommandAction.Views.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/xaml/behaviors"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
Title="{Binding Title}" Height="350" Width="525">
<Grid>
<ListBox ItemsSource="{Binding Items}" SelectionMode="Single">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<prism:InvokeCommandAction Command="{Binding SelectedCommand}"
CommandParameter="{Binding MyParameter}"
TriggerParameterPath="AddedItems" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
</Grid>
</Window>
AutoEnable
AutoEnable属性指定是否应根据ICommand.CanExecute的结果自动启用或禁用关联元素。默认值为true,因为这是最常用的用法。
<Window x:Class="UsingInvokeCommandAction.Views.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/xaml/behaviors"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
Title="{Binding Title}" Height="350" Width="525">
<Grid>
<ListBox ItemsSource="{Binding Items}" SelectionMode="Single">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<prism:InvokeCommandAction Command="{Binding SelectedCommand}"
CommandParameter="{Binding MyParameter}"
TriggerParameterPath="AddedItems"
AutoEnable="true" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
</Grid>
</Window>
完整代码示例
有关完整的代码示例,请访问GitHub中的Prism-Samples-Wpf仓库,并参阅29-InvokeCommandAction。