ActionMessage事件绑定是个人觉的算是CM的精髓了,比如说我在View里面放个button,我们要在他的click事件里面写东西,怎么写.如果是WPF我们直接在CS里面写就可以.但是CM不行,他给我们提供了这个机制.写法如下:EventName指定是什么事件,MethodName是方法名称,<cal:Parameter Value="{Binding ElementName=list, Path=SelectedItems}" />这个方法的参数我们用的事绑定listview控件的选中项.
<Window x:Class="Erp.Views.Test.HelloDataView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cal="http://www.caliburnproject.org"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" Title="HelloData" Height="350" Width="1000"
xmlns:page="clr-namespace:Erp.Views.UserControls"
WindowStartupLocation="CenterScreen">
<Grid>
<StackPanel Background="LightBlue">
<ListView x:Name="list" Height="130" Margin="5"
cal:Message.Attach="[Event MouseDoubleClick]=[Action Update($this.SelectedItem)]"
>
<ListView.View>
<GridView>
<GridViewColumn x:Name="gvc">
<GridViewColumn.Header>
<CheckBox cal:Action.Target="{Binding ElementName=list}" cal:Message.Attach="[Event Checked]=[Action SelectAll];
[Event Unchecked]=[Action UnselectAll]">全选</CheckBox>
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="ck" Tag="{Binding Path=ID}"
IsChecked="{Binding IsSelected,RelativeSource={RelativeSource AncestorType=ListViewItem}}"></CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="姓名" Width="60" DisplayMemberBinding="{Binding Path=Name}"/>
<GridViewColumn Header="年龄" Width="60" DisplayMemberBinding="{Binding Path=Age}"/>
<GridViewColumn Header="性别" Width="60" DisplayMemberBinding="{Binding Path=Sex}"/>
<GridViewColumn Header="入职日期" Width="80" DisplayMemberBinding="{Binding Path=Time}"/>
<GridViewColumn Header="国籍" Width="60" DisplayMemberBinding="{Binding Path=Controy}"/>
</GridView>
</ListView.View>
</ListView>
<Button Content="删除" Width="100" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cal:ActionMessage MethodName="Delete">
<cal:Parameter Value="{Binding ElementName=list, Path=SelectedItems}" />
</cal:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
</Grid>
</Window>
CM为ActionMessage提供了更为强大的Attach功能,可以写成这样
<Button cal:Message.Attach="[Event Click]=[Action Delete(list.SelectedItems)]" Content="删除" Width="100"/>
如果我们还需要其他事件,直接叠加就可以
<Button cal:Message.Attach="[Event Click]=[Action Delete(list.SelectedItems)]"
cal:Message.Attach="[Event 事件名称]=[Action 调用的方法(参数)]"
Content="删除" Width="100"/>