0

我有一个自定义控件,它是一个带有用于清除文本框的嵌入式按钮的文本框。按钮连接到以ApplicationCommands.Delete目标为文本框的命令。这完全符合预期。

我的问题是我有另一个使用此自定义文本框的控件,并且需要知道何时ApplicationCommands.Delete执行,以便它可以在默认操作之上运行自己的操作。我试图添加到CommandBindings,但这不起作用。有没有办法在父级接收命令通知UIElement

文本框自定义控件 XAML

<UserControl.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Delete" Executed="ClearTextBoxExecuted" />
</UserControl.CommandBindings>
...
<Button Command="ApplicationCommands.Delete" CommandTarget="{Binding ElementName=FilteredTextBox}"

后面的代码:

private void ClearTextBoxExecuted(object sender, ExecutedRoutedEventArgs e)
{
  if (!(e.OriginalSource is TextBox)) return;
  var textBox = e.OriginalSource as TextBox;
  textBox.Clear();
  if (TextCleared != null)
    TextCleared(sender, null);
}

由于程序集资源 Uri 约束,Xaml 包装了文本框

<UserControl x:Class="CopyOfEnhancedTextboxForLocalInheritance" Name="MainWrapper">
<textBoxes:EnhancedTextBox x:Name="EnhancedTextBox"...

热键选择器文本框:

public class HotKeySelectorBox : CustomTextBox
{
  void SetupCommands()
  {
    var deleteCommand = new CommandBinding(ApplicationCommands.Delete,
                                         (sender, args) => 
                                           DeleteSelection());
    deleteCommand.CanExecute +=
    (sender, args) =>
      {
        args.Handled = true;
        args.CanExecute = !(Text == String.Empty || Text == "none");
      };
    EnhancedTextBox.CommandBindings.Add(deleteCommand);
    CommandBindings.Add(deleteCommand);
  }
}
4

0 回答 0