• 在WPF中设置Command命令的触发行为


    1.在可点击的控件中比如:按钮 、单选框、复选框中绑定了command命令后,单击该控件会执行command命令,那么如何设置,双击或者键盘回车键触发command命令呢?

    ViewModel:
    public class MainViewModel { public Command Btn1Command { get; set; } public MainViewModel() { Btn1Command = new Command(); Btn1Command.DoExcute = Dobtn1Cmd; } private void Dobtn1Cmd(object obj) { int a = 0; } }

    xaml:
    <Grid> <StackPanel> <Button Content="btn1"> <Button.InputBindings > <MouseBinding Command="{Binding Btn1Command}" MouseAction="RightClick"></MouseBinding> </Button.InputBindings> </Button> </StackPanel> </Grid>
    这里MouseAction 的行为总共7种,左键、右键、中键、滚轮滚动、左键双击、右键双击、中键双击,设置键盘触发要设置KeyBinding
    2.在WPF中布局控件(如:grid)无法直接使用command进行命令绑定,只能通过事件的方式进行绑定 这里我们可以通过Behaviors实现
    在WPF项目中添加NuGet包 :Microsoft.Xaml.Behaviors.Wpf
    xmal:
    <Window x:Class="WpfAppTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfAppTest" xmlns:b="http://schemas.microsoft.com/xaml/behaviors" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <b:Interaction.Triggers> <b:EventTrigger EventName="MouseMove"> <b:InvokeCommandAction Command="{Binding Btn1Command}"></b:InvokeCommandAction> </b:EventTrigger> </b:Interaction.Triggers> </Grid> </Window>

  • 相关阅读:
    都是CSS惹的祸
    Ruby简介
    网络攻击利用DedeCms漏洞
    ASP.NET验证技术详解
    一个低级错误引发的血案
    FCKeditor配置和精简【附源码】
    邮件发送详解
    Timer定时器的设计实例详解
    常用的加密算法MD5、SHA1
    JS日历控件集合附效果图、源代码
  • 原文地址:https://www.cnblogs.com/wilsonNet/p/16555047.html
Copyright © 2020-2023  润新知