WPF 任意事件绑定

任意事件绑定到Command的集中方法

1.InvokCommandAction

在NuGet中添加 System.Windows.Interactivity.WPF

在Xaml中添加引用

 xmlns:i="http://schemas.microsoft.com/expression/2010/interactions"
 xmlns:ii="http://schemas.microsoft.com/expression/2010/interactivity"

使用InvokCommandAction,能传递控件本身,但没有EventArgs e参数

EventName是想触发的事件名

InvokCommandAction中的 Command绑定的命令,需要写在ViewModel或者Model中,必须是一个继承自ICommand接口的类的实例

<StackPanel>
     <ComboBox Height="50" >
          <i:Interaction.Triggers>
               <!--无法传递参数,适用于不使用参数的事件,InvokeCommandAction在interactivity中-->
                   <i:EventTrigger EventName="SelectionChanged">
                      <i:InvokeCommandAction Command="{Binding mainModel.BtnAddCommand }" 
                                         CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
     <ComboBoxItem Content="1"/>
    <ComboBoxItem Content="2"/>
    <ComboBoxItem Content="3"/>
    <ComboBoxItem Content="4"/>
  </ComboBox>
<StaclPanel>

2.使用CallMethodAction,可以传递参数

<ComboBox Height="50" >
                <i:Interaction.Triggers>
                    <!-- CallMethodAction在interactions中 -->
                    <i:EventTrigger EventName="SelectionChanged">
                        <ii:CallMethodAction TargetObject="{Binding}" 
                                           MethodName="ComboBox_SelectionChanged"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <ComboBoxItem Content="1"/>
                <ComboBoxItem Content="2"/>
                <ComboBoxItem Content="3"/>
                <ComboBoxItem Content="4"/>
  </ComboBox>

ViewModel中:

把原来写在MainWindow.cs中的事件挪到vm中,需要把事件的访问权限改成public,默认是private的

InvokeCommandAtion和CallMethodAction的区别?

InvokeCommandAtion:单纯的是用Command进行触发,并且VM中需要声明ICommand方法

CallMethodAction,其实是反射的方法,targetObject的binding是直接绑定DataContext,程序在执行时从DataContext中寻找MethodName的方法体,不需要写命令

在使用 System.Windows.Interactivity.WPF时,会提示让使用Microsoft.Xaml.Behaviors.Wpf
在xaml中删除System.Windows.Interactivity.WPF的引用

 添加对Microsoft.Xaml.Behaviors.Wpf的引用

xmlns:i="http://schemas.microsoft.com/xaml/behaviors"

InvokeCommandAtion和CallMethodAction都在这一个命名空间中

3.EventToCommand

上面2中情况是在不使用mvvmlight情况下,

在mvvmlight下,可以使用EventToCommand,在命名空间下:

 xmlns:cmd="http://www.galasoft.ch/mvvmlight"

还需要使用

 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 

不能使用

xmlns:i="http://schemas.microsoft.com/xaml/behaviors"

xaml中:

<Button Name="b2" Content="带参数按钮">
        <i:Interaction.Triggers>
              <i:EventTrigger EventName="Click">
                   <cmd:EventToCommand  Command="{Binding testcmd2}" PassEventArgsToCommand="True"/>
                </i:EventTrigger>
          </i:Interaction.Triggers>
</Button>

PassE ventArgstoCommand="true"决定是否传递参数

VewModel或者Model中定义一个命令

public class MYViewModel
 {
        public ICommand testcmd { get; set; }
        public ICommand testcmd2 { get; set; }
        public MYViewModel()
        {
            testcmd = new RelayCommand(new Action(ButtonClick)) ;
            testcmd2 =new RelayCommand<EventArgs>(new Action<EventArgs>(ButtonClick2)) ;
        }
        private void ButtonClick2(EventArgs obj)
        {    
        }
        private void ButtonClick()
        {  
        }
}

testcmd不带参数。testcmd2带参数

obj就是EventArgs

 

posted @ 2022-08-24 22:16  薛定谔的小灯泡  阅读(1843)  评论(1编辑  收藏  举报