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将失效

  • 相关阅读:
    wp7 db
    wp7 webpair
    wp7 弹出动画
    wp7 执行独立存储任务
    黑马程序员第二阶段JAVA网络编程第24天
    黑马程序员第二阶段图形用户界面第22天(1)
    黑马程序员第三阶段Java基础加强第26天
    黑马程序员第三阶段Java基础加强第25天
    黑马程序员第二阶段Java高级IO第21天
    黑马程序员第二阶段JAVA网络编程第23天
  • 原文地址:https://www.cnblogs.com/wpfworld/p/3432230.html
Copyright © 2011-2022 走看看