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;
            }
        }
    }
  • 相关阅读:
    Run keyword if
    sublime Text如何取消两栏窗口?
    WIN7右键在目录当前打开命令行Cmd窗口
    电脑用HDMI线分屏后,耳机或音箱没声音之完美解决!
    如何解决failed to load the jni shared library问题
    APP测试点
    PHP 根据ip判断其所属地址
    thinkPHP git上传的时候,总是有些文件传不上去,.gitignore解析
    Integrity constraint violation: 1052 Column 'status' in where clause is ambiguous
    tp5导出生成pdf
  • 原文地址:https://www.cnblogs.com/dxmfans/p/9434613.html
Copyright © 2011-2022 走看看