WPF中的Command事件绑定

在项目中使用Command绑定能够使我们的代码更加的符合MVVM模式。不了解的同学可能不清楚,只有继承自ButtonBase类的元素才可以直接绑定Command(Button、CheckBox、RadioButton等)

<Button Content="Normal" Command="{Binding NormalEventCommand}" ></Button>

如果我们要处理Label或者其他的一些控件,那么只能在走事件:

<Label Content="Label Event" MouseDoubleClick="Label_MouseDoubleClick_1"></Label>

这样的话,我们不得不在窗体类中处理事件代码和部分逻辑,这样就无法得到干净的MVVM模式了,那么我们应该怎么做呢?

Blend为我们提供了解决方案,我们安装Blend以后,便可以获取到System.Windows.Interactivity.dll,添加该dll到项目引用

<Window x:Class="WpfApplication1.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:vm="clr-namespace:WpfApplication1"
        Title="Window2" Height="124" Width="214">
    <Window.DataContext>
        <vm:Window2ViewModel />
    </Window.DataContext>
    <Grid>
        <Button Name="btn" Content="Button" Height="33" HorizontalAlignment="Left" Margin="40,24,0,0" VerticalAlignment="Top" Width="109">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <i:InvokeCommandAction Command="{Binding Command1}" CommandParameter="10" />
                </i:EventTrigger>
                <i:EventTrigger EventName="MouseMove">
                    <i:InvokeCommandAction Command="{Binding Command2}" CommandParameter="{Binding ElementName=btn}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>
    </Grid>
</Window>

需要注意Window标签中的

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity",这里相当于后台的using System;之类的加载程序集的功能

这样的我们,我们便可以处理所有目标元素的事件。

同样我们面对另外一个问题,我们是否可以绑定按键事件,并处理特定的键值命令呢?答案是肯定的,使用KeyBinding就可以了,同样还可以使用MouseBinding进行鼠标事件的处理。

        <TextBox Text="test">
            <TextBox.InputBindings>
                <KeyBinding Key="S" Modifiers="Alt" Command="{Binding KeyEventCommand}"></KeyBinding>
                <MouseBinding Gesture="RightClick" MouseAction="LeftClick"  Command="{Binding MouseEventCommand}"></MouseBinding>
            </TextBox.InputBindings>
        </TextBox>
        <Label Content="test">
            <Label.InputBindings>
                <KeyBinding Key="S" Modifiers="Alt" Command="{Binding KeyEventCommand}"></KeyBinding>
                <MouseBinding MouseAction="LeftClick" Command="{Binding MouseEventCommand}"></MouseBinding>
            </Label.InputBindings>
        </Label>

这里我们针对TextBox和Label进行了InputBindings的绑定。

需要注意的是:

1.InputBindings是个绑定集合InputBindingCollection,可以有多个MouseBinding和多个KeyBinding

2.KeyBinding绑定对象需要可聚焦,这里的Label由于无法聚焦,所以无法响应Alt+S事件

3.MouseBinding中Gesture的优先级高于MouseAction,当Gesture设置以后,LeftClick将失效

时间: 2024-08-01 23:02:29

WPF中的Command事件绑定的相关文章

WPF中一个控件绑定另一个控件的属性

原文:WPF中一个控件绑定另一个控件的属性 如同一个Grid中的一个按钮根据另一个按钮的显示与否作出不同的响应: 绑定的时候通过ElementName来指定控件 <Grid Margin="50,130"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="40"/> </Grid.ColumnDefinitions>

WPF中的Command命令详解

在WPF中使用命令的步骤很简单 1.创建命令 2.绑定命令 3.设置命令源 4.设置命令目标 WPF中命令的核心是System.Windows.Input.ICommand接口,所有命令对象都实现了此接口.当创建自己的命令时,不能直接实现ICommand接口,而是要使用System.Windows.Input.RouteCommand类,该类已经实现了ICommand接口,所有WPF命令都是RouteCommand类的实例.在程序中处理的大部分命令不是RoutedCommand对象,而是Rout

WPF 中的 loaded 事件和 Initialized 事件

在 WPF 中, 控件有 Loaded 和 Initialized 两种事件. 初始化和加载控件几乎同时发生, 因此这两个事件也几乎同时触发. 但是他们之间有微妙且重要的区别. 这些区别很容易让人误解. 这里介绍我们设计这些事件的背景. (不仅适用于 Control 类, 同样在通用类如 FrameworkElement 和 FrameworkContentElement 类也适用.) 下面是个小故事: Initialized 事件只说: 这个元素已经被构建出来,并且它的属性值都被设置好了,所以

jquery中的DOM事件绑定与解绑

在jquery事件中有时候有的事件只需要在绑定后有效触发一次,当通过e.target判断触发条件有效触发后解除绑定事件,来避免多次无效触发和与未知情况造成冲突. 这时候就要用到了jquery中的事件绑定与事件解绑. 1 $(document).on('click.endEdit',function (e) { 2 if(!$(e.target).hasClass('datagrid-cell')&&e.target.tagName!="TD"){ //事件发生后的判断条

WPF中的Command

Command的意义: 在开发过程当中,会有很多按钮需要实现同样的功能.因此可以采用Command将很多操作绑定到统一逻辑. 可以通过CanExecute能够实现禁用/启用控件 Command的四个主要概念 Command Command Source Command Target Command Binding Command: 是要执行的操作.通过ICommand接口实现(方法:Execute/CanExecute,事件:CanExecuteChange)注:Command本身不包含处理逻辑

javascript中常用的事件绑定方法

我们要想让 JavaScript 对用户的操作作出响应,首先要对 DOM 元素绑定事件处理函数.所谓事件处理函数,就是处理用户操作的函数,不同的操作对应不同的名称. 在JavaScript中,有三种常用的绑定事件的方法: 在DOM元素中直接绑定: 在JavaScript代码中绑定: 绑定事件监听函数. 一. 在DOM元素中直接绑定 这里的DOM元素,可以理解为HTML标签.JavaScript支持在标签中直接绑定事件,语法为:    onXXX="JavaScript Code" 其中

在WPF中如何使用RelativeSource绑定

在WPF绑定的时候,指定绑定源时,有一种办法是使用RelativeSource. 这种办法的意思是指当前元素和绑定源的位置关系. 第一种关系: Self 举一个最简单的例子:在一个StackPanel中,有一个TextBlock. <TextBlock FontSize="18" FontWeight="Bold" Margin="10" Background="Red" Width="80" Hei

jquery中四个事件绑定方式(bind,live,delegate,on)

bind() 简要描述 bind()向匹配元素添加一个或多个事件处理器. 使用方式 $(selector).bind(event,data,function) event:必需项:添加到元素的一个或多个事件,例如 click,dblclick等: 单事件处理:例如 $(selector).bind("click",data,function); 多事件处理:1.利用空格分隔多事件,例如 $(selector).bind("click dbclick mouseout"

WPF中多个RadioButton绑定到一个属性

如图样: 在View中: <RadioButton IsChecked="{Binding Option, Converter={cvt:EnumToBooleanConverter},ConverterParameter={x:Static enum:RadionButtonOptions.One}}" Content="One" DockPanel.Dock="Top"/> <RadioButton IsChecked=&q