Prism之InvokeCommandAction的TriggerParameterPath和CommandParameter的用法

Prism库中的InvokeCommandAction(写成prism:InvokeCommandAction)是比较重要的类,相对于Interactivity库中的InvokeCommandAction(写成i:InvokeCommandAction),比较重要的是增加了TriggerParameterPath属性,该属性可以实现将触发命令的事件对象本身事件对象之属性传递给ViewModel中的命令对象。

TriggerParameterPath是对应于EventArgs中的属性路径,比如用于SelectionChanged事件,则对应于SelectionChangedEventArgs的属性的字符串,如果写 TriggerParameterPath="AddedItems",则指SelectionChangedEventArgs.AddedItems对象。CommandParameter和TriggerParameterPath不同,它就是直接传过去的参数,可以是简单的字符串,也可以是绑定的数据对象。

无废话,上代码:

(一)示例1:使用TriggerParameterPath例子

  <ListBox Grid.Row="1" Margin="5" ItemsSource="{Binding Items}" SelectionMode="Single">
            <i:Interaction.Triggers>
                <!-- This event trigger will execute the action when the corresponding event is raised by the ListBox. -->
                <i:EventTrigger EventName="SelectionChanged">
                    <!-- This action will invoke the selected command in the view model and pass the parameters of the event to it. -->
                    <prism:InvokeCommandAction Command="{Binding SelectedCommand}"  TriggerParameterPath="AddedItems"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </ListBox>

在ViewModel中定义命令 public DelegateCommand<object[]> SelectedCommand { get; private set; },即把SelectionChangedEventArgs.AddedItems的值传给DelegateCommand的object[]对象,关于DelegateCommad的创建如下:

  public MainWindowViewModel()
        {

           ...

            // This command will be executed when the selection of the ListBox in the view changes.
            SelectedCommand = new DelegateCommand<object[]>(OnItemSelected);
        }

        private void OnItemSelected(object[] selectedItems)
        {
            if (selectedItems != null && selectedItems.Count() > 0)
            {
               //Do Work here
            }
        }

如果写成TriggerParameterPath=""或者不写TriggerParameterPath属性,则表示把事件对象本身传过去作为CommandParameter,比如如果要传鼠标事件,则就写成TriggerParameterPath=""或不写TriggerParameterPath属性,然后在ViewModel的命令处理程序中

        private void OnMouseDownCommandHandler(object mouseDownEventArgs)
        {
            if (mouseDownEventArgs != null && mouseDownEventArgs is MouseButtonEventArgs args)
            {
                //获取鼠标点的相对位置
               Point p= args.GetPosition((IInputElement)args.Source);  //这些用到Source对象,代表触发事件的界面元素
                
                // 补充具体的工作代码
            }
        }

 

(二)示例2:使用CommandParameter例子

  <ListBox Grid.Row="1" Margin="5" Name="_listBox" ItemsSource="{Binding Items}"  SelectionMode="Single">
            <i:Interaction.Triggers>
                <!-- This event trigger will execute the action when the corresponding event is raised by the ListBox. -->
                <i:EventTrigger EventName="SelectionChanged">
                    <!-- This action will invoke the selected command in the view model and pass the parameters of the event to it. -->
                    <prism:InvokeCommandAction Command="{Binding SelectedCommand}"  CommandParameter={Binding Path=SelectedItem,ElementName=_listBox}/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </ListBox>

这个与上面的方法的区别在于,使用了InvokeCommandAction对象的CommandParameter属性传递SelectedItem对象, ViewModel中的代码如下:

  public DelegateCommand<object> SelectedCommand { get; private set; }

        public MainWindowViewModel()
        {

            // This command will be executed when the selection of the ListBox in the view changes.
            SelectedCommand = new DelegateCommand<object>(OnItemSelected);
        }

        private void OnItemSelected(object selectedItem)
        {
            if (selectedItem != null && selectedItem is string s)
            {
                //Do work here
            }
        }

看一下InvokeCommandAction 的定义,就清楚了。定义如下:

  //
    // 摘要:
    //     Trigger action that executes a command when invoked. It also maintains the Enabled
    //     state of the target control based on the CanExecute method of the command.
    public class InvokeCommandAction : TriggerAction<UIElement>
    {
        //
        // 摘要:
        //     Dependency property identifying if the associated element should automaticlaly
        //     be enabled or disabled based on the result of the Command's CanExecute
        public static readonly DependencyProperty AutoEnableProperty;
        //
        // 摘要:
        //     Dependency property identifying the command to execute when invoked.
        public static readonly DependencyProperty CommandProperty;
        //
        // 摘要:
        //     Dependency property identifying the command parameter to supply on command execution.
        public static readonly DependencyProperty CommandParameterProperty;
        //
        // 摘要:
        //     Dependency property identifying the TriggerParameterPath to be parsed to identify
        //     the child property of the trigger parameter to be used as the command parameter.  
       //也就是说,从触发参数的TriggerParameterPath中解析出响应的对象,用作command Parameter
        public static readonly DependencyProperty TriggerParameterPathProperty;

        public InvokeCommandAction();

        //
        // 摘要:
        //     Gets or sets whther or not the associated element will automatically be enabled
        //     or disabled based on the result of the commands CanExecute
        public bool AutoEnable { get; set; }
        //
        // 摘要:
        //     Gets or sets the command to execute when invoked.
        public ICommand Command { get; set; }
        //
        // 摘要:
        //     Gets or sets the command parameter to supply on command execution.
        public object CommandParameter { get; set; }
        //
        // 摘要:
        //     Gets or sets the TriggerParameterPath value.
        public string TriggerParameterPath { get; set; }

        //
        // 摘要:
        //     Public wrapper of the Invoke method.
        public void InvokeAction(object parameter);
        //
        // 摘要:
        //     Executes the command
        //
        // 参数:
        //   parameter:
        //     This parameter is passed to the command; the CommandParameter specified in the
        //     CommandParameterProperty is used for command invocation if not null.
        protected override void Invoke(object parameter);
        //
        // 摘要:
        //     This method is called after the behavior is attached. It updates the command
        //     behavior's Command and CommandParameter properties if necessary.
        protected override void OnAttached();
        //
        // 摘要:
        //     Sets the Command and CommandParameter properties to null.
        protected override void OnDetaching();
    }

 

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值