zoukankan      html  css  js  c++  java
  • 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将失效

  • 相关阅读:
    NOIP2010普及组T3 接水问题 ——S.B.S.
    【NOIP提高组2015D2T1】uva 714 copying books【二分答案】——yhx
    【NOIP合并果子】uva 10954 add all【贪心】——yhx
    #include &lt;NOIP2009 Junior&gt; 细胞分裂 ——using namespace wxl;
    #include &lt;NOIP2008 Junior&gt; 双栈排序 ——using namespace wxl;
    NOIP2010普及组 三国游戏 -SilverN
    NOIP2009 提高组T3 机器翻译 解题报告-S.B.S
    NOIP2010提高组乌龟棋 -SilverN
    NOIP2010提高组 机器翻译 -SilverN
    UVa 297 Quadtrees -SilverN
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14209208.html
Copyright © 2011-2022 走看看