简体   繁体   English

WPF在用户控件上剪切,复制,粘贴功能

[英]WPF cut, copy, paste functionality on a user control

I have a canvas on which you can add UserControls (consists of images and textboxes) 我有一个画布,可以在其中添加UserControls(由图像和文本框组成)

Im trying to implement cut, copy, paste functionality on these UserControls, so the context menu is attatched to a UserControl which deals with images for example. 我试图在这些UserControl上实现剪切,复制,粘贴功能,因此上下文菜单将附加到处理图像的UserControl上。 A user right clicks here and from the context menu picks copy for instance how would I then go about implementing so they can paste this on to the canvas. 用户在此处单击鼠标右键,然后从上下文菜单中选择复制,然后我将如何实施,以便他们可以将其粘贴到画布上。

Can anyone point me in the right direction... 谁能指出我正确的方向...

This can be done with RoutedCommands. 这可以通过RoutedCommands完成。 A full overview is at MSDN: Commanding Overview 完整的概述位于MSDN: 命令概述

The short version is this: when a command source (ie a menu item) wants to execute a command, an event is raised. 简短的版本是这样的:当命令源 (即菜单项)想要执行命令时,将引发一个事件。 That event is handled by the nearest command binding . 该事件由最近的命令绑定处理。 Cut/copy/paste commands are already included with WPF, and certain elements (namely, text boxes) already include command bindings for them. 剪切/复制/粘贴命令已经包含在WPF中,并且某些元素(即文本框)已经包含了它们的命令绑定。

You can define a menu item like this: 您可以定义一个菜单项,如下所示:

<MenuItem Header="Copy" Command="ApplicationCommands.Copy" />

And add a command binding to the UserControl like this: 并向用户控件添加命令绑定,如下所示:

<UserControl.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Copy"
                    Executed="Copy_Executed" />
</UserControl.CommandBindings>

And define the Copy_Executed method with the ExecutedRoutedEventHandler signature in the UserControl's code-behind. 并在后面的UserControl代码中使用ExecutedRoutedEventHandler签名定义Copy_Executed方法。

Then of course do the same thing for ApplicationCommands.Paste within the canvas. 然后,当然要对画布中的ApplicationCommands.Paste执行相同的操作。

It's up to you whether you want to handle the data within your own application, or use the clipboard. 是否要在自己的应用程序中处理数据还是使用剪贴板取决于您。 If you're working with images, WPF has a Clipboard class which can work with BitmapSource objects (if you have an Image element, chances are its Source is already a BitmapSource ). 如果您正在使用图像,则WPF具有一个Clipboard类,可以与BitmapSource对象一起使用(如果您具有Image元素,则其Source已经是BitmapSource )。

Firstly, a well designed MVVM application can make copy/paste of user controls much simpler since it will turn to serialize/deserialize CLR objects to Clipboard. 首先,一个设计良好的MVVM应用程序可以使用户控件的复制/粘贴更加简单,因为它将转向将CLR对象序列化/反序列化到剪贴板。 WPF will handle user control creation by itself after deserialization. WPF将在反序列化之后自行处理用户控件的创建。

If your application does not implement MVVM. 如果您的应用程序未实现MVVM。 You can use XamlWriter/XamlReader to save user controls to XAML and recreate them by yourself. 您可以使用XamlWriter / XamlReader将用户控件保存到XAML并自行重新创建它们。 An example: 一个例子:

        StringBuilder outstr = new StringBuilder();

        //this code need for right XML fomating 
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.OmitXmlDeclaration = true;
        XamlDesignerSerializationManager dsm =
          new XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings));
        //this string need for turning on expression saving mode 
        dsm.XamlWriterMode = XamlWriterMode.Expression;
        XamlWriter.Save(control, dsm);

        //Read control from XAML
        var frameObject = XamlReader.Parse(outstr.ToString()) as UserControl;
        if (frameObject != null)
            stackPanel.Children.Add(frameObject);

For the part of how to put the XAML string or serialized stream into Clipboard, you can refer to MSDN. 有关如何将XAML字符串或序列化流放入剪贴板的部分,可以参考MSDN。

Hope it can help. 希望能有所帮助。

If you want to bind the command (as @nmclean explains ) from code, you can use: 如果要绑定代码中的命令(如@nmclean所述 ),则可以使用:

CommandBindings.Add(new CommandBinding(
    ApplicationCommands.Copy,
    (sender, args) => { /* logic here */ }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM