InvokeCommandAction传递多个参数
命名空间:
xmlns:i=“http://schemas.microsoft.com/xaml/behaviors”
一般写法,一般只能传递一个参数
<i:Interaction.Triggers>
<i:EventTrigger EventName="LostFocus">
<i:InvokeCommandAction Command="{Binding DataContext.LostFocusCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" PassEventArgsToCommand="True" />
</i:EventTrigger>
<i:EventTrigger EventName="GotFocus">
<i:InvokeCommandAction Command="{Binding DataContext.GotFocusCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" PassEventArgsToCommand="True" />
</i:EventTrigger>
<!--CommandParameter="itemGrid",这种后台参数为itemGrid-->
<i:EventTrigger EventName="MouseEnter">
<i:InvokeCommandAction Command="{Binding DataContext.MouseEnterCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
CommandParameter="itemGrid"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
解析Command
Command="{Binding DataContext.LostFocusCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
Binding DataContext.LostFocusCommand——》DataContext,View对应的ViewModel (这里用的是Prism框架)
传递的是 ViewModel 的 DelegateCommand
Command="{Binding DataContext.LostFocusCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
Binding RelativeSource
RelativeSource={RelativeSource AncestorType={x:Type UserControl}} 指向 源对象
RelativeSource首先要3个关键参数
AncestorType=需要查找的类型。比如Grid
AncestorLevel= 需要向上查找几级。
Path=我们找到的元素需要绑定的属性。
<!--嵌套Grid-->
<Grid x:Name="G0" Margin="12" Background="Red">
<TextBlock Text="In this Grid0 container"/>
<Grid x:Name="G1" Margin="12" Background="Blue">
<TextBlock Text="In this Grid1 container"/>
<Grid x:Name="G2" Margin="12" Background="Yellow">
<TextBlock Text="In this Grid2 container"/>
<Grid x:Name="G3" Margin="12" Background="Beige">
<StackPanel>
<TextBlock Text="In this Grid3 container"/>
<!--AncestorType=我们需要查找的类型。比如Grid-->
<!--AncestorLevel= 我们需要向上查找几级-->
<!--Path=我们找到的元素需要绑定的属性。-->
<TextBlock Name="ces" Text="{Binding RelativeSource={RelativeSource AncestorType=Grid,AncestorLevel=1},Path=Name}"/>
</StackPanel>
</Grid>
</Grid>
</Grid>