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
}
}