• WPF 中的 Command 命令


    <Window x:Class="CommandDemo.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:CommandDemo"

            mc:Ignorable="d"

            Title="MainWindow" Height="450" Width="800">

        <Grid>

            <StackPanel x:Name="stackpanel" >

                <Button x:Name="button1"  Content="send command"></Button>

                <TextBox x:Name="textboxA" Margin="5,0" Height="100"></TextBox>

                <TextBox x:Name="textboxB" Margin="5,0" Height="100"></TextBox>

            </StackPanel>

        </Grid>

    </Window>

    C# 代码

     public partial class MainWindow : Window

        {

            public MainWindow()

            {

                InitializeComponent();

                this.InitializeCommand();

            }

            private RoutedCommand ClearCmd = new RoutedCommand("clear", typeof(MainWindow));

            private void InitializeCommand()

            {

                this.ClearCmd.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Alt));

                this.button1.Command = this.ClearCmd;

                this.button1.CommandTarget = this.textboxA;

                CommandBinding cb = new CommandBinding();

                cb.Command = this.ClearCmd;

                cb.CanExecute += new CanExecuteRoutedEventHandler(cb_CanExecute);

                cb.Executed += new ExecutedRoutedEventHandler(cb_Executed);

                this.stackpanel.CommandBindings.Add(cb);

            }

            private void cb_Executed(object sender, ExecutedRoutedEventArgs e)

            {

                this.textboxA.Clear();

            }

            private void cb_CanExecute(object sender, CanExecuteRoutedEventArgs e)

            {

                e.CanExecute = true;

            }

        }

  • 相关阅读:
    Javascript、C#、php、asp、python 等语言的链式操作的实现
    根据C# 事件思想来实现 php 事件
    initerrlog: 无法打开错误日志文件 'D:Program FilesMicrosoft SQL ServerMSSQL10_50.MSSQLSERVERMSSQLLog 解决办法
    64位直接加载个img 标签的src
    各种脚本语言变量作用域总结
    数据库设计14个技巧【转】
    基于Jquery 简单实用的弹出提示框
    C# dll 事件执行 js 回调函数
    php 配置xdebug
    sqlserver 构架与性能优化
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14532108.html
Copyright © 2020-2023  润新知