Click here to Skip to main content
Click here to Skip to main content

Copy and Paste in WPF User Control

By , 7 Aug 2012
 

Introduction

This is something that kept me wondering for quite a while when I was trying to create a user control which was able to receive clipboard commands by itself. The base XAML setup was straightforward:

<UserControl x:Class="UserControlCommands.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" 
             d:DesignWidth="300"
             >
    <UserControl.CommandBindings>
        <CommandBinding Command="Paste" Executed="CommandBinding_PasteExecuted"/>
        <CommandBinding Command="Copy" Executed="CommandBinding_CopyExecuted"/>
    </UserControl.CommandBindings>
</UserControl>

Well, nothing happened. The commandbindings would not fire because commands are only routed to UI elements that have the focus. And as you can see, there's nothing inside the usercontrol that itself would be able to gain the focus (e.g. a TextBox object).

The first tip I found on this topic was to set the user control attribute Focusable="true". This by itself wasn't helpful, the user control would still not gain the focus when clicking into it. After adding IsTabStop="true", I was at least able to tab into the user control - and now the events would fire on CTRl+C or CTRL+V without any more effort (Copy and Paste are enumerators of the ApplicationCommands enumeration and therefore the application is able to raise them automatically).

However, this still is not sufficient. I wanted to make the user control to gain the focus by mouse click and then be able to paste information to it, thinking there must be some attribute or something that can be set in XAML. It turned out there is not. However I found the following thread on exactly the opposite topic:

Though I did not like this (thinking there must be some way to do this entirely in XAML), I gave in and added a MouseDown event calling this.Focus(). And then there was only one last step necessary: A UI element needs a background to receive mouse clicks, so I set background="aquamarine".

Finally. Here's the complete sample:

XAML

<UserControl x:Class="UserControlCommands.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" 
             d:DesignWidth="300"
             IsTabStop="True"
             MouseDown="UserControl_MouseDown"
             Focusable="True"
             Background="Aquamarine"
             >
    <UserControl.CommandBindings>
        <CommandBinding Command="Paste" Executed="CommandBinding_Executed"/>
        <CommandBinding Command="Copy" Executed="CommandBinding_Executed"/>
    </UserControl.CommandBindings>
</UserControl>   

Code Behind

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace UserControlCommands
{
    public partial class MyUserControl : UserControl
    {
        public MyUserControl()
        {
            InitializeComponent();
        }

        private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Clipboard operation occured!");
        }

        private void UserControl_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.Focus();
        }
    }
}  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Doc Lobster
Software Developer
Germany Germany
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralIt worksmemberAlok Chauhan18-Oct-12 17:33 
GeneralMy vote of 4memberdstar_2007@yahoo.co.in16-Jul-12 5:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130617.1 | Last Updated 7 Aug 2012
Article Copyright 2012 by Doc Lobster
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid