有没有办法在WPF文本框中添加复制事件的处理程序?

问题描述 投票:0回答:1

是否有与文本框控件的 HTML onCopy 事件等效的 WPF?我想在将文本复制到剪贴板之前进行一些处理。

c# .net wpf wpf-controls
1个回答
0
投票

您可以使用

CommandManager.PreviewExecuted
路由事件。当调用 RoutedCommand 上的 Execute 方法时会引发此问题。

在构造函数中:

CommandManager.AddPreviewExecutedHandler(textBox, onPreviewExecuted);

功能:

private void onPreviewExecuted(object sender, ExecutedRoutedEventArgs e)
{
    // Check if the executed command is a copy command
    if (e.Command == ApplicationCommands.Copy)
    {
        // Custom processing here
        // ...

        // Mark the event as handled to prevent the default copy operation
        e.Handled = true;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.