WPF-MVVM-InvokeCommandAction传递多个参数

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>
   
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值