如何在WPF文本框上覆盖复制命令?

时间:2009-10-08 14:51:48

标签: wpf-controls routed-commands

我想覆盖WPF TextBox的RoutedUICommand“Copy”的行为。

是否可以不创建从TextBox继承的新TextBoxExtended类?

我已达到这一点,但现在我有点失落。

Private Sub tbSource_PreviewExecuted(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)

        Dim commandName = DirectCast(e.Command, Input.RoutedUICommand).Text

        If commandName = "Copy" Then

        End If

End Sub

你知道如何继续吗?

1 个答案:

答案 0 :(得分:4)

您可以向文本框添加命令绑定以处理“复制”命令。例如,像这样:

<StackPanel>
  <TextBlock x:Name="TextBox">
    <TextBlock.CommandBindings>
        <CommandBinding Command="{x:Static ApplicationCommands.Copy}"
                        Executed="CommandBinding_Executed"/>
    </TextBlock.CommandBindings>
  </TextBlock>
  <Button Content="Copy" 
          Command="{x:Static ApplicationCommands.Copy}"
          CommandTarget="{Binding ElementName=TextBox}"/>
</StackPanel>
相关问题