美文网首页
WPF基本命令系统使用

WPF基本命令系统使用

作者: 玉米须须 | 来源:发表于2019-07-24 10:18 被阅读0次
<Window x:Class="CommandTest.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:CommandTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid x:Name="aGrid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="236*"/>
            <ColumnDefinition Width="559*"/>
        </Grid.ColumnDefinitions>
        <Button x:Name="abutton" Content="Button" HorizontalAlignment="Left" Margin="186.046,62,0,0" VerticalAlignment="Top" Width="75" Grid.Column="1"/>
        <TextBox x:Name="aTextBox" HorizontalAlignment="Left" Height="23" Margin="186.046,126,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" Grid.Column="1"/>

    </Grid>
</Window>

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            initializeCommand();
        }

        //声明并定义命令
        private RoutedCommand clearCmd = new RoutedCommand("clear", typeof(MainWindow));

        private void initializeCommand() {
            //把命令赋值给发送者并指定快捷键
            abutton.Command = clearCmd;
            clearCmd.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Alt));

            //指定命令目标
            abutton.CommandTarget = aTextBox;

            //创建命令关联
            CommandBinding cb = new CommandBinding();
            cb.Command = clearCmd;
            cb.CanExecute += new CanExecuteRoutedEventHandler(cb_canExecute);
            cb.Executed += new ExecutedRoutedEventHandler(cb_execute);

            //把命令关联在外围控件上
            aGrid.CommandBindings.Add(cb);
        }

        //探测命令是否被执行时,该命令被调用 
        void cb_canExecute(object sender, CanExecuteRoutedEventArgs e) {
            if (string.IsNullOrEmpty(aTextBox.Text))
            {
                e.CanExecute = false;
            }
            else {
                e.CanExecute = true;
            }

            //避免继续向上传降低程序性能
            e.Handled = true;
        }

        //当命令到达目标后,此方法被调用
        void cb_execute(object sender, ExecutedRoutedEventArgs e) {
            aTextBox.Clear();

            //避免继续向上传降低程序性能
            e.Handled = true;
        }
    }
}

相关文章

  • WPF基本命令系统使用

  • WPF基本命令系统使用2

    界面上有两个按钮,一个用于新建teacher,一个用于新建student,都使用new命令,那么就是通过Comma...

  • CefSharp For WPF基本使用

    Nuget引用 直接搜索安装CefSharp.Wpf即可,因为CefSharp.Wpf依赖于其他三个包,所以会自动...

  • WPF DataGrid 基本使用

    效果如下: 1. 设计DataGrid展示的数据模型类 public class Song { public i...

  • 【WPF】Command 自定义命令

    在使用 MVVM 结构的 WPF 程序中,ViewModel 对 View 的响应是通过 Command (命令)...

  • linux学习笔记-day14-查看磁盘

    linux系统中使用df命令查看磁盘的容量、使用容量、剩余容量等 ,默认使用kb为单位显示。命令的输出基本上会存在...

  • Linux命令行

    一、基本的bash shell命令 1.浏览文件系统 遍历目录 在Linux文件系统上,可以使用切换目录命令cd将...

  • GPG 基本命令总结

    GPG加密系统 一、基本命令使用 Use "gpg --full-generate-key" for a full...

  • Linux 命令行复习

    在公司基本使用的是Windows系统,导致Linux系统的命令不是很熟悉; 记录一下Linux基本的文件操作!其实...

  • Linux系统监控命令详解

    1. top命令 top命令经常用来监控 Linux 的系统状况,比如cpu、内存的使用,程序员基本都知道这个命令...

网友评论

      本文标题:WPF基本命令系统使用

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