• WPF(命令)


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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.Navigation;
    using System.Windows.Shapes;
    
    namespace WpfCommand
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                InitializeCommand();
            }
    
            // 声明命令
            private RoutedCommand clearCmd = new RoutedCommand("Clear", typeof(MainWindow));
    
    
            private void InitializeCommand()
            {
                // 命令源
                this.btn.Command = clearCmd;
                this.clearCmd.InputGestures.Add(new KeyGesture(Key.C,ModifierKeys.Alt));
    
                // 命令目标
                this.btn.CommandTarget = this.txt;
    
                // 命令绑定
                CommandBinding cb = new CommandBinding();
                cb.Command = this.clearCmd;
                cb.CanExecute += new CanExecuteRoutedEventHandler(cb_CanExecute);
                cb.Executed += new ExecutedRoutedEventHandler(cb_Execute);
    
                // 安置到外围控件上
                this.stackpanel.CommandBindings.Add(cb);
            }
    
            private void cb_CanExecute(object sender, CanExecuteRoutedEventArgs args)
            {
                if (string.IsNullOrEmpty(this.txt.Text))
                {
                    args.CanExecute = false;
                }
                else
                {
                    args.CanExecute = true;
                }
    
                args.Handled = true;
            }
    
            private void cb_Execute(object sender, ExecutedRoutedEventArgs args)
            {
                this.txt.Clear();
                args.Handled = true;
            }
        }
    }
    

    -----------------------------------------------------------

    优化代码段

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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.Navigation;
    using System.Windows.Shapes;
    
    namespace WpfCommand
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                InitializeCommand();
            }
    
            // 声明命令
            private RoutedCommand clearCmd = new RoutedCommand("Clear", typeof(MainWindow));
    
    
            private void InitializeCommand()
            {
                // 命令源
                this.btn.Command = clearCmd;
                this.clearCmd.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Alt));
    
                //// 命令目标
                //this.btn.CommandTarget = this.txt;
    
                //// 命令绑定
                //CommandBinding cb = new CommandBinding();
                //cb.Command = this.clearCmd;
                //cb.CanExecute += new CanExecuteRoutedEventHandler(cb_CanExecute);
                //cb.Executed += new ExecutedRoutedEventHandler(cb_Execute);
    
                // 安置到外围控件上
                this.CommandBindings.Add(new CommandBinding(clearCmd,cb_Execute,cb_CanExecute));
            }
    
            private void cb_CanExecute(object sender, CanExecuteRoutedEventArgs args)
            {
                if (string.IsNullOrEmpty(this.txt.Text))
                {
                    args.CanExecute = false;
                }
                else
                {
                    args.CanExecute = true;
                }
    
                args.Handled = true;
            }
    
            private void cb_Execute(object sender, ExecutedRoutedEventArgs args)
            {
                this.txt.Clear();
                args.Handled = true;
            }
        }
    }
    


  • 相关阅读:
    [bzoj1576] [Usaco2009 Jan]安全路经Travel
    [坑][poj2396]有上下界的最大流
    bzoj1458 士兵占领
    [Ahoi2013]差异
    bzoj2424 [HAOI2010]订货
    bzoj1741 [Usaco2005 nov]Asteroids 穿越小行星群
    bzoj2251 [2010Beijing Wc]外星联络
    bzoj1977 [BeiJing2010组队]次小生成树 Tree
    bzoj2729 [HNOI2012]排队
    bzoj1925 [Sdoi2010]地精部落
  • 原文地址:https://www.cnblogs.com/wjchang/p/3671517.html
Copyright © 2020-2023  润新知