Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6679827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:24:14+00:00 2026-05-26T04:24:14+00:00

I have successfully used a few custom commands using MVVM-Light, but I want my

  • 0

I have successfully used a few custom commands using MVVM-Light, but I want my application to respond to the standard ApplicationCommands, not just at a Window level, but at a detailed item level as well.

I have a TreeView that I want to be able to copy and paste nodes in. Each TreeViewItem has its own ViewModel, and they are displayed via HierarchicalDataTemplates in XAML as there are several different types. I have implemented methods to copy, paste, as well as CanCopy and CanPaste on my ViewModel classes. If appropriate, I could implement MVVM-Light RelayCommands pointing to these easily enough, but that doesn’t seem right.

I would like to access the commands using a menu, Ctrl+C and Ctrl+V, or eventually a context menu. I also don’t want to break copy/paste functionality for other elements in my UI, such as TextBoxes. It seems appropriate to use the built-in ApplicationCommands for this purpose. However, I am only seeing examples of these being handled in a UserControl code-behind. I don’t have (or otherwise need) a UserControl, nor is that really following MVVM.

Is there a way I can bind ApplicationCommand.Copy and ApplicationCommand.Paste commands to my ViewModels, i.e., in the data templates?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:24:15+00:00Added an answer on May 26, 2026 at 4:24 am

    I have resolved this using Behaviors attached to the TreeView. The TreeViewItems or Templates to not seem to get the commands routed to them. Fortunately, the TreeView also has a SelectedItem property that can be used to get the ViewModel!

    (Behaviors are conceptually similar to the solution in the link in @Natxo’s answer, but it doesn’t resolve everything.)

    The Behavior class:

    public class TreeViewClipboardBehavior : Behavior<TreeView>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
    
            CommandBinding CopyCommandBinding = new CommandBinding(
                ApplicationCommands.Copy,
                CopyCommandExecuted,
                CopyCommandCanExecute);
            AssociatedObject.CommandBindings.Add(CopyCommandBinding);
    
            CommandBinding CutCommandBinding = new CommandBinding(
                ApplicationCommands.Cut,
                CutCommandExecuted,
                CutCommandCanExecute);
            AssociatedObject.CommandBindings.Add(CutCommandBinding);
    
            CommandBinding PasteCommandBinding = new CommandBinding(
                ApplicationCommands.Paste,
                PasteCommandExecuted,
                PasteCommandCanExecute);
            AssociatedObject.CommandBindings.Add(PasteCommandBinding);
        }
    
        private void CopyCommandExecuted(object target, ExecutedRoutedEventArgs e)
        {
            NestingItemTreeViewModelBase item = AssociatedObject.SelectedItem as NestingItemTreeViewModelBase;
            if (item != null && item.CanCopyToClipboard)
            {
                item.CopyToClipboard();
                e.Handled = true;
            }
        }
    
        private void CopyCommandCanExecute(object target, CanExecuteRoutedEventArgs e)
        {
            NestingItemTreeViewModelBase item = AssociatedObject.SelectedItem as NestingItemTreeViewModelBase;
            if (item != null)
            {
                e.CanExecute = item.CanCopyToClipboard;
                e.Handled = true;
            }
        }
    
        private void CutCommandExecuted(object target, ExecutedRoutedEventArgs e)
        {
            NestingItemTreeViewModelBase item = AssociatedObject.SelectedItem as NestingItemTreeViewModelBase;
            if (item != null && item.CanCutToClipboard)
            {
                item.CutToClipboard();
                e.Handled = true;
            }
        }
    
        private void CutCommandCanExecute(object target, CanExecuteRoutedEventArgs e)
        {
            NestingItemTreeViewModelBase item = AssociatedObject.SelectedItem as NestingItemTreeViewModelBase;
            if (item != null)
            {
                e.CanExecute = item.CanCutToClipboard;
                e.Handled = true;
            }
        }
    
    
        private void PasteCommandExecuted(object target, ExecutedRoutedEventArgs e)
        {
            NestingItemTreeViewModelBase item = AssociatedObject.SelectedItem as NestingItemTreeViewModelBase;
            if (item != null && item.CanPasteFromClipboard)
            {
                item.PasteFromClipboard();
                e.Handled = true;
            }
        }
    
        private void PasteCommandCanExecute(object target, CanExecuteRoutedEventArgs e)
        {
            NestingItemTreeViewModelBase item = AssociatedObject.SelectedItem as NestingItemTreeViewModelBase;
            if (item != null)
            {
                e.CanExecute = item.CanPasteFromClipboard;
                e.Handled = true;
            }
        }
    }
    

    The XAML

    <TreeView Grid.Row="2" ItemsSource="{Binding SystemTreeRoot}">
        <i:Interaction.Behaviors>
            <local:TreeViewClipboardBehavior/>
        </i:Interaction.Behaviors>
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:MyViewModel}" ItemsSource="{Binding Children}">
                <!-- Template content -->
            </HierarchicalDataTemplate>
    </TreeView>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have successfully used IntentService a few times, but in the current app, I
I have been trying to post using RestKit after I have successfully used it
I have used java mail(com.sun.mail.smtp) from my struts project. Mail is sent successfully but
For a few months I have been successfully using David Justices Default Button example
I have been using ASP.NET MVC for the past few months, after successfully ignoring
I have two iOS devices finding each other successfully using the Bonjour API, but
I have successfully used the Excel and Word addin templates in Visual studio 2008
I have created a REST web service and successfully used it with a client.
I need to used dynamic order query in mysql and i have successfully achieved
I used to have this kind of simple JSON data and I could successfully

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.