2013/08/31 C# WPF 学习笔记

WPF 命令的简单范例

XAML代码

<Window x:Class="CommandTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
    <Grid Margin="3,3,3,3" Name="g_1">
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <TextBox Name="txt_1" Grid.Row="0" Margin="2,2,2,2"></TextBox>
        <TextBox Name="txt_2" Grid.Row="2" Margin="2,2,2,2"></TextBox>
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button Name="btn_send" Height="28" Width="80" Content="发送(S)" Grid.Column="0"></Button>
            <Button Name="btn_clear" Height="28" Width="80" Content="清空(C)" Grid.Column="1"></Button>
        </Grid>
    </Grid>
</Window>

CS代码

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 CommandTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        #region 全局变量

        /// <summary>
        /// 声明并定义命令  发送
        /// </summary>
        private RoutedCommand sendCmd = new RoutedCommand("Send", typeof(MainWindow));
        /// <summary>
        /// 声明并定义命令  清空
        /// </summary>
        private RoutedCommand clearCmd = new RoutedCommand("Clear", typeof(MainWindow));

        #endregion

        #region 函数

        /// <summary>
        /// 构造函数
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            InitializeCommand();
        }
        /// <summary>
        /// 命令初始化
        /// </summary>
        private void InitializeCommand()
        {
            //把命令复制给命令源(发送者)并指定快捷键
            btn_send.Command = this.sendCmd;
            this.sendCmd.InputGestures.Add(new KeyGesture(Key.S,ModifierKeys.Alt));
            btn_clear.Command = this.clearCmd;
            this.clearCmd.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Alt));

            //指定命令目标
            btn_send.CommandTarget = this.txt_2;
            btn_clear.CommandTarget = this.txt_2;

            //创建命令关联
            CommandBinding cb_send = new CommandBinding();
            cb_send.Command = this.sendCmd;
            cb_send.CanExecute +=new CanExecuteRoutedEventHandler(cb_send_CanExecute);
            cb_send.Executed += new ExecutedRoutedEventHandler(cb_send_Executed);
            CommandBinding cb_clear = new CommandBinding();
            cb_clear.Command = this.clearCmd;
            cb_clear.CanExecute += new CanExecuteRoutedEventHandler(cb_clear_CanExecute);
            cb_clear.Executed += new ExecutedRoutedEventHandler(cb_clear_Executed);
        
            //把命令关联安置在外围控件上
            g_1.CommandBindings.Add(cb_send);
            g_1.CommandBindings.Add(cb_clear);
        }

        #endregion

        #region 事件
        /// <summary>
        /// 当探测命令是否可以执行时,此方法被调用 [探测发送]
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void cb_send_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txt_2.Text)) { e.CanExecute = false; }
            else { e.CanExecute = true; }
            //避免继续向上传而降低程序性能
            e.Handled = true;
        }
        /// <summary>
        /// 当命令送达目标后,此方法被调用 [发送]
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void cb_send_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txt_1.Text)) { txt_1.Text += txt_2.Text; }
            else { txt_1.Text += "\n" + txt_2.Text; }
            txt_2.Clear();
            //避免继续向上传而降低程序性能
            e.Handled = true;
        }
        /// <summary>
        /// 当探测命令是否可以执行时,此方法被调用 [探测清空]
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void cb_clear_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txt_2.Text)) { e.CanExecute = false; }
            else { e.CanExecute = true; }
            //避免继续向上传而降低程序性能
            e.Handled = true;
        }
        /// <summary>
        /// 当命令送达目标后,此方法被调用 [清空]
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void cb_clear_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            txt_2.Clear();
            //避免继续向上传而降低程序性能
            e.Handled = true;
        }
        #endregion
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值