Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
130
Overriding XamDiagram Copy/Cut/Paste Functionality
posted

I am using a XamDiagram with data binding (ItemsSource and ConnectionsSource are bound to Nodes, Connections lists in view model), and I need to be able to copy/cut paste diagram items.

I know this functionality is built into the XamDiagram control, but I get some very strange behavior when copying and pasting diagram items, mostly stemming from the fact that I am binding the diagram items to data in my view model, more specifically that each diagram item has some unique information (GUID, plus some other stuff) that should not be copied directly, but instead would require some more advanced logic during copy/paste.

So I'm trying to figure out a way to override the Copy/Cut/Paste functionality and use my own commands instead. Is there any way to do this?

Parents
No Data
Reply
  • 1500
    Verified Answer
    Offline posted

    Hello Andrew,

    Thank you for posting on our forums.

    You can bind the copy/paste command and replace them with your own, modifying the functionality of those commands:

    <ig:XamDiagram.CommandBindings>
                    <CommandBinding Command="{x:Static ApplicationCommands.Copy}" 
                                    CanExecute="CanCopy" 
                                    Executed="CustomCopy"/>
                    <CommandBinding Command="{x:Static ApplicationCommands.Paste}" 
                                    CanExecute="CanPaste"
                                    Executed="CustomPaste"/>
    </ig:XamDiagram.CommandBindings>

    Or you can hook to the PreviewKeyDown event and check if Ctrl+V has been pressed:

    private void xamDiagram1_PreviewKeyDown(object sender, KeyEventArgs e)
            {
                var diagram = sender as XamDiagram;
                if (e.Key == Key.V && Keyboard.Modifiers == ModifierKeys.Control)
                {
                    e.Handled = true;
                    if (diagram.SelectedItems != null)
                    {
                        var node = diagram.SelectedItems.First() as DiagramNode;
                        DiagramNode newNode = new DiagramNode()
                        {
                            DataContext = node.DataContext,
                            ShapeType = node.ShapeType
                        };
                        newNode.Position = new Point(5, 125);
                        diagram.Items.Add(newNode);
                    }    
                }
            }

    And finally, I can also suggest looking up the ItemAdding/Added events which will trigger every time a new node/connection has been added to the Items collection of the Diagram.

    Should you need further assistance, please let me know.

    Sincerely,
    Tihomir Tonev
    Associate Software Developer
    Infragistics

Children
No Data