命令

作者: 落地成佛 | 来源:发表于2019-10-11 08:57 被阅读0次

    一、概述

    • 创建命令类
    • 声明命令实例
    • 指定命令源
    • 指定命令目标
    • 设置命令关联

    二、使用

    2.1 例子

       public Window28()
            {
                InitializeComponent();
                InitializeCommand();
            }
            //声明并定义命令
            private RoutedCommand rouutedCommand = new RoutedCommand("Clear",typeof(Window28));
            private void InitializeCommand()
            {
                //把命令赋值给命令源,并定义快捷键
                this.btn1.Command = rouutedCommand;
                this.rouutedCommand.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Alt));
                //指定命令目标
                this.btn1.CommandTarget = txtA;
    
                //创建命令关联
                CommandBinding commandBinding = new CommandBinding();
                commandBinding.Command = rouutedCommand;//只关注与rouutedCommand相关的命令
                commandBinding.CanExecute += new CanExecuteRoutedEventHandler(cb_CanExecute);
                commandBinding.Executed += new ExecutedRoutedEventHandler(cb_Execute);
                //把命令关联安置在外围控件上
                this.sp1.CommandBindings.Add(commandBinding);
            }
            //当命令到达目标之后,此方法被调用
            private void cb_Execute(object sender, ExecutedRoutedEventArgs e)
            {
                txtA.Clear();
                //避免事件继续向上传递而降低程序性能
                e.Handled = true;
            }
            //当探测命令是否可执行的时候该方法会被调用
            private void cb_CanExecute(object sender,CanExecuteRoutedEventArgs e)
            {
                if (string.IsNullOrEmpty(txtA.Text))
                {
                    e.CanExecute = false;
                }
                else
                {
                    e.CanExecute = true;
                }
                //避免事件继续向上传递而降低程序性能
                e.Handled = true;
            }
    
       <StackPanel Background="LightBlue" x:Name="sp1">
            <Button Content="Send Command" x:Name="btn1" Margin="5"></Button>
            <TextBox x:Name="txtA" Margin="5,0" Height="200"></TextBox>
        </StackPanel>
    

    2.2 默认命令库

    • ApplicationCommands
    • ComponentCommands
    • NavigationCommands
    • MediaCommands
    • EditingCommands
       <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="24" />
                <RowDefinition Height="4" />
                <RowDefinition Height="24" />
                <RowDefinition Height="4" />
                <RowDefinition Height="24" />
                <RowDefinition Height="4" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <!--命令和命令参数-->
            <TextBlock  HorizontalAlignment="Left" Name="textBlock1" Text="Name:" VerticalAlignment="Center" Grid.Row="0"/>
            <TextBox x:Name="txtName" Margin="60,0,0,0" Grid.Row="0"></TextBox>
            <Button Content="New Teacher" Grid.Row="2" Command="New" CommandParameter="Teacher"></Button>
            <Button Content="New Student" Grid.Row="4" Command="New" CommandParameter="Student"></Button>
            <ListBox Grid.Row="6" x:Name="lbInfos">
                
            </ListBox>
           
        </Grid>
        <!--为窗体添加CommandBinding-->
        <Window.CommandBindings>
            <CommandBinding Command="New" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed">
                
            </CommandBinding>
        </Window.CommandBindings>
    
       private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
            {
                if (string.IsNullOrEmpty(txtName.Text))
                {
                    e.CanExecute = false;
                }
                else
                {
                    e.CanExecute = true;
                }
                //路由终止,提高系统性能
                e.Handled = true;
            }
    
            private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
            {
                if (e.Parameter.ToString() == "Student")
                {
                    this.lbInfos.Items.Add(string.Format("New Student:{0} 好好学习,天天向上。",txtName.Text));
                }
                else if(e.Parameter.ToString()=="Teacher")
                {
                    this.lbInfos.Items.Add(string.Format("New Teacher:{0} 学而不厌,诲人不倦。", txtName.Text));
                }
                //路由终止,提高系统性能
                e.Handled = true;
            }
    

    相关文章

      网友评论

        本文标题:命令

        本文链接:https://www.haomeiwen.com/subject/dcealctx.html