WPFはRoutedCommandカスタムコマンドを使用

1494 ワード

主なコードは次のとおりです.
/// <summary>
///        。
/// </summary>
RoutedCommand ClearCommand = new RoutedCommand("Clear", typeof(MainWindow));

/// <summary>
///      。    2014-7-30 06:23:10
/// </summary>
void InitializeCommand()
{
    //         。
    this.ClearCommand.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Alt));

    //         。
    this.Button1.Command = this.ClearCommand;

    //       。
    this.Button1.CommandTarget = this.TextBoxA;

    //                。
    CommandBinding cb = new CommandBinding();
    cb.Command = this.ClearCommand;
    cb.CanExecute += new CanExecuteRoutedEventHandler(cb_CanExecute);
    cb.Executed += new ExecutedRoutedEventHandler(cb_Executed);
    this.StackPanel1.CommandBindings.Add(cb);
}

/// <summary>
///              。    2014-7-30 06:27:16
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void cb_Executed(object sender, ExecutedRoutedEventArgs e)
{
    this.TextBoxA.Clear();
    e.Handled = true;
}

/// <summary>
///                 。    2014-7-30 06:26:20
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void cb_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    if (string.IsNullOrEmpty(this.TextBoxA.Text) == true) e.CanExecute = false;
    else e.CanExecute = true;

    e.Handled = true;
}