Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 允许';粘贴数据';进入WPF文本框_C#_Wpf_Clipboard - Fatal编程技术网

C# 允许';粘贴数据';进入WPF文本框

C# 允许';粘贴数据';进入WPF文本框,c#,wpf,clipboard,C#,Wpf,Clipboard,我试图截取粘贴到WPF文本框中的数据 例如,用户使用Windows剪贴工具创建屏幕截图, 它会自动将图像数据放在剪贴板上。这里的想法是 允许用户在文本框上简单地按CTRL+V,以便我可以截取它,检查它是否正确 数据,然后我想用它做什么就做什么 public class PasteBehavior : Behavior<UIElement> { protected override void OnAttached() { base.OnAttached(

我试图截取粘贴到WPF文本框中的数据

例如,用户使用Windows剪贴工具创建屏幕截图, 它会自动将图像数据放在剪贴板上。这里的想法是 允许用户在文本框上简单地按CTRL+V,以便我可以截取它,检查它是否正确 数据,然后我想用它做什么就做什么

public class PasteBehavior : Behavior<UIElement>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        DataObject.AddPastingHandler(AssociatedObject, new DataObjectPastingEventHandler(OnPaste));
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
    }

    private void OnPaste(object sender, DataObjectPastingEventArgs e)
    {
        if (e.SourceDataObject.GetDataPresent(DataFormats.Text))
            return;

        var formats = e.SourceDataObject.GetFormats();
        foreach (var format in formats)
            Console.WriteLine(format);
    }
}

并删除了PreviewKeyDownHandler。

您可以使用
CommandManager.PreviewExecuted
CommandManager.PreviewCanExecute
路由事件来处理粘贴逻辑

例如,假设当用户试图将图像粘贴到文本框中时,您希望接受剪贴板中的图像。首先,定义处理这两个事件的方法:

    private void onPreviewCanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        // In this case, we just say it always can be executed (only for a Paste command), but you can 
        // write some checks here
        if (e.Command == ApplicationCommands.Paste)
        {
            e.CanExecute = true;
            e.Handled = true;
        }
    }

    private void onPreviewExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        // If it is a paste command..
        if (e.Command == ApplicationCommands.Paste)
        {
            // .. and the clipboard contains an image
            if (Clipboard.ContainsImage())
            {
                // proccess it somehow
                e.Handled = true;
            }

        }
    }
然后,您必须将这些方法与路由事件相关联(例如,这可能在构造函数中):

而且它应该与键盘快捷键和菜单“按钮”一起使用

处理PreviewCanExecute事件非常重要。默认情况下,文本框只接受文本作为“可粘贴”内容,因此您需要以某种方式标记该内容才能粘贴它

编辑: 此外,如果可以的话,从事件中删除“侦听器”也是一种很好的做法。在使用行为时,可以通过在行为中重写“OnDetaching”方法来实现这一点。如果事件不是弱事件,这可以防止内存泄漏:

    protected override void OnDetaching()
    {
        base.OnDetaching();
        CommandManager.RemovePreviewExecutedHandler(myTextBox, onPreviewExecuted);
        CommandManager.RemovePreviewCanExecuteHandler(myTextBox, onPreviewCanExecute);
    }

让我试一试minute@TimothyP请参阅我的最后一个注释,关于在您不使用Hnx时删除事件侦听器。。。这几次让我很恼火:请锁定一个代码片段,您正在显式地处理CTRL+V--永远不要这样做例如,用户可能已将粘贴配置为不同的组合键。必须有一个更广泛的“粘贴”消息从操作系统,你可以处理。
    private void onPreviewCanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        // In this case, we just say it always can be executed (only for a Paste command), but you can 
        // write some checks here
        if (e.Command == ApplicationCommands.Paste)
        {
            e.CanExecute = true;
            e.Handled = true;
        }
    }

    private void onPreviewExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        // If it is a paste command..
        if (e.Command == ApplicationCommands.Paste)
        {
            // .. and the clipboard contains an image
            if (Clipboard.ContainsImage())
            {
                // proccess it somehow
                e.Handled = true;
            }

        }
    }
CommandManager.AddPreviewExecutedHandler(myTextBox, onPreviewExecuted);
CommandManager.AddPreviewCanExecuteHandler(myTextBox, onPreviewCanExecute);
    protected override void OnDetaching()
    {
        base.OnDetaching();
        CommandManager.RemovePreviewExecutedHandler(myTextBox, onPreviewExecuted);
        CommandManager.RemovePreviewCanExecuteHandler(myTextBox, onPreviewCanExecute);
    }