Hi,

I have a WPF Grid with multiple controls like TextBox, ComboBox, CheckBox etc. and their properties like Text, SelectedItem, IsChecked etc. are present inside the view model bound to the view perfectly.
I want to copy the values of all the controls to the Clipboard (may be) and want to paste the corresponding values of these controls again and similarly, I want to clear all the values on clear button click.

I would appreciate if anyone could please suggest a possible way of achieving this functionality.
Thank you very much for your help and time.

I am sorry to make response after two weeks.

I do not understand how could you implement Cut, Copy, and Paste on Combobox or other objects except Textbox / RichTextBox.

WPF uses a number of prebuilt commands. The designers of wpf realized that every application is likely to have a large number of commands and that many commands are common to many different applications. For example, all document based applications will have their own versions of the New, Open, Save, Cut, Copy and Paste commands. To save you the work of greating those commands, wpf includes a basic command library that's stocked with more than 100 commands. You can bind them directly by using Command property of the object.

If you get three buttons for Cut, Copy and Paste, you can bind the respective commands with their command property. Like

<Button Command="ApplicationCommands.Cut" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"
        Margin="5,0,5,0" Width="80"/>
<Button Command="ApplicationCommands.Copy" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"
        Margin="5,0,5,0" Width="80"/>
<Button Command="ApplicationCommands.Paste" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"
        Margin="5,0,5,0" Width="80"/>

Using these commands you have no need to track the disable/enable the buttons, when needed. WPF will do it atomatically. You have no need to do any extra codes to perform their jobs.

I am posting here some simple codes. You can examine it by pasting in your project.

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="1st Text Project" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <TextBox Grid.Row="0"
                 AcceptsReturn="True" 
                 Name="txtArea" FontSize="14"
                 BorderBrush="Blue" VerticalScrollBarVisibility="Auto"
                 HorizontalScrollBarVisibility="auto"></TextBox>

        <StackPanel Grid.Row="1"
                    Height="30"
                    Margin="5,10,5,5"
                    Orientation="Horizontal" 
                    HorizontalAlignment="Center" 
                    FocusManager.IsFocusScope="True" >
            <Button Command="ApplicationCommands.Cut" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"
                    Margin="5,0,5,0" Width="80"/>
            <Button Command="ApplicationCommands.Copy" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"
                    Margin="5,0,5,0" Width="80"/>
            <Button Command="ApplicationCommands.Paste" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"
                    Margin="5,0,5,0" Width="80"/>
        </StackPanel>
    </Grid>
</Window>

Here I attach FocusManager.IsFocusScope property with stackpanel to create a new focus scope. This tells WPF to look the element in the parant's focus scope when the command is triggered.
Hope it can help you.

This article has been dead for over six months. Start a new discussion instead.