简体   繁体   中英

WPF cut, copy, paste functionality on a user control

I have a canvas on which you can add UserControls (consists of images and textboxes)

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. 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. A full overview is at MSDN: Commanding Overview

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.

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.

Then of course do the same thing for ApplicationCommands.Paste within the canvas.

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 ).

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. WPF will handle user control creation by itself after deserialization.

If your application does not implement MVVM. You can use XamlWriter/XamlReader to save user controls to XAML and recreate them by yourself. 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.

Hope it can help.

If you want to bind the command (as @nmclean explains ) from code, you can use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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