zoukankan      html  css  js  c++  java
  • WPF 通过CommandBinding捕获命令

    RoutedCommand与业务逻辑无关,业务逻辑是通过CommandBinding来实现

    using System;

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;


    namespace Wpf180706
    {
        /// <summary>
        /// Interaction logic for Window4.xaml
        /// </summary>
        public partial class Window4 : Window
        {
            public Window4()
            {
                InitializeComponent();
                InitializeCommond();
            }


            private RoutedCommand clearCmd = new RoutedCommand("Clear", typeof(Window));




            private void InitializeCommond()
            {
                this.btn.Command = clearCmd;
                clearCmd.InputGestures.Add(new KeyGesture(Key.C,ModifierKeys.Alt));
                //this.btn.CommandTarget = txt;目标可以指定也可以由WPF根据焦点判断
                CommandBinding cb = new CommandBinding();
                cb.Command = clearCmd;
                cb.Executed += cb_Executed;
                cb.CanExecute += cb_CanExecute;
                this.CommandBindings.Add(cb);



            }


            void cb_CanExecute(object sender, CanExecuteRoutedEventArgs e)
            {
                if (string.IsNullOrEmpty(txt.Text))
                {
                    e.CanExecute = false;
                }
                else
                {
                    e.CanExecute = true;
                }


            }


            void cb_Executed(object sender, ExecutedRoutedEventArgs e)
            {
                
                txt.Clear();
                e.Handled = true;
            }
        }
    }
  • 相关阅读:
    Linux-nmap
    MongoDb注意事项
    HTML如何转XTML
    Centos 64位 Install certificate on apache 即走https协议
    CentOS 下搭建部署独立SVN服务器全程详解(5.5)
    LNMP安装与配置
    64位CentOS 6.0下搭建LAMP环境
    Apache遇到的问题:APR not found
    超详细LAMP环境搭建
    偏方治百病
  • 原文地址:https://www.cnblogs.com/dxmfans/p/9434613.html
Copyright © 2011-2022 走看看