• 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;
            }
        }
    }
  • 相关阅读:
    望图知意BlogPatrol又挂了
    看你知道不知道之-制作数据字典
    mount: none already mounted or /cgroup busy
    [zz]OpenStack中虚拟机的监控
    guestfs
    Ubuntu 10.04 wine 中运行某个门
    [zz]Wine完全使用指南——从基本到高级
    [zz]史上最快消息内核——ZeroMQ
    [zz]OpenStack Compute(Nova)功能分析
    [zz][技术] 完美使用 WINE 来运行 RTX ,QQ
  • 原文地址:https://www.cnblogs.com/dxmfans/p/9434613.html
Copyright © 2020-2023  润新知