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
在这里插入图片描述
在这里插入图片描述
该文章转载自博客:
https://www.cnblogs.com/1024E/p/16622471.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF中,我们可以通过使用鼠标事件和属性来实现任意控件的拖动,并且可以设置边界限制。 首先,我们需要在控件的鼠标按下事件(MouseDown)中添加事件处理程序。在事件处理程序中,我们可以捕获鼠标相对于控件的位置,并将其保存为偏移量。 接下来,在控件的鼠标移动事件(MouseMove)中,我们可以使用偏移量来计算控件应该移动的新位置。同时,我们还需要检查新的位置是否超出了我们设置的边界限制,如果超出了边界,我们可以将控件的位置限制在边界中。 最后,在鼠标释放事件(MouseUp)中,我们需要停止控件的拖动状态。 下面是一段实现控件拖动和边界限制的示例代码: ```csharp private bool isDragging = false; // 是否正在拖动 private Point offset; // 鼠标相对控件的偏移量 private void Control_MouseDown(object sender, MouseButtonEventArgs e) { isDragging = true; offset = e.GetPosition(sender as UIElement); (sender as UIElement).CaptureMouse(); // 捕获鼠标 } private void Control_MouseMove(object sender, MouseEventArgs e) { if (isDragging) { var control = sender as UIElement; var currentPosition = e.GetPosition(control.Parent as UIElement); // 计算新的位置 double newX = currentPosition.X - offset.X; double newY = currentPosition.Y - offset.Y; // 设置边界限制 if (newX < 0) newX = 0; if (newX + control.ActualWidth > (control.Parent as FrameworkElement).ActualWidth) newX = (control.Parent as FrameworkElement).ActualWidth - control.ActualWidth; if (newY < 0) newY = 0; if (newY + control.ActualHeight > (control.Parent as FrameworkElement).ActualHeight) newY = (control.Parent as FrameworkElement).ActualHeight - control.ActualHeight; // 移动控件 Canvas.SetLeft(control, newX); Canvas.SetTop(control, newY); } } private void Control_MouseUp(object sender, MouseButtonEventArgs e) { isDragging = false; (sender as UIElement).ReleaseMouseCapture(); // 释放鼠标 } ``` 以上代码假设我们有一个名为Control的控件,我们可以在该控件上添加MouseDown、MouseMove和MouseUp事件事件处理程序,并将其到相应的方法上。 在这个示例中,我们使用Canvas作为控件的父容器,并根据Canvas的尺寸来设置边界限制,你也可以根据自己的需求来调整边界的设置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值