Posted by: Zeeshan Amjad | February 9, 2010

Using Command Converter for built in commands


We have already seen the example of using type converter. We have already seen the usage of built in commands using command target. Now we are going to make a same program, but this time using the commands converter in C# code. Here is a class diagram of command converter.

CommandConverter

Now lets make the same program again but defining the command with command converter not in XAML. Here is a piece of code to use the command converter for built in commands.

  1: CommandConverter cv = TypeDescriptor.GetConverter(typeof(ICommand)) as CommandConverter;
  2: 
  3: if (cv != null)
  4: {
  5:     btnCopy.Command = (ICommand)cv.ConvertFromString("ApplicationCommands.Copy");
  6:     btnPaste.Command = (ICommand)cv.ConvertFromString("ApplicationCommands.Paste");
  7: }
  8: 

Here we have to define the name of the button, so we can use that name in C# code. Here is complete XAML code of the program.

  1: <Window x:Class="WpfCommand.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Command Binding" Height="300" Width="300">
  5:     <Grid Background="AliceBlue">
  6:         <Grid.RowDefinitions>
  7:             <RowDefinition Height="2*"/>
  8:             <RowDefinition Height="2*"/>
  9:             <RowDefinition/>
 10:         </Grid.RowDefinitions>
 11:         <Grid.ColumnDefinitions>
 12:             <ColumnDefinition/>
 13:             <ColumnDefinition/>
 14:         </Grid.ColumnDefinitions>
 15:         <TextBox Name="source" Margin="5" Grid.Row="0" Grid.ColumnSpan="2"/>
 16:         <TextBox Name="destination" Margin="5" Grid.Row="1" Grid.ColumnSpan="2"/>
 17:         <Button Name="btnCopy" Margin="5" Grid.Row="2" Grid.Column="0" 
 18:                 Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"
 19:                 CommandTarget="{Binding ElementName=source}"/>
 20:         <Button Name="btnPaste" Margin="5" Grid.Row="2" Grid.Column="1" 
 21:                 Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"
 22:                 CommandTarget="{Binding ElementName=destination}"/>
 23:     </Grid>
 24: </Window>
 25: 

 

Here is complete C# code of the program.

 

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Text;
  5: using System.Windows;
  6: using System.Windows.Controls;
  7: using System.Windows.Data;
  8: using System.Windows.Documents;
  9: using System.Windows.Input;
 10: using System.Windows.Media;
 11: using System.Windows.Media.Imaging;
 12: using System.Windows.Navigation;
 13: using System.Windows.Shapes;
 14: using System.ComponentModel;
 15: 
 16: namespace WpfCommand
 17: {
 18:     /// <summary>
 19:     /// Interaction logic for Window1.xaml
 20:     /// </summary>
 21:     public partial class Window1 : Window
 22:     {
 23:         public Window1()
 24:         {
 25:             InitializeComponent();
 26: 
 27:             CommandConverter cv = TypeDescriptor.GetConverter(typeof(ICommand)) as CommandConverter;
 28: 
 29:             if (cv != null)
 30:             {
 31:                 btnCopy.Command = (ICommand)cv.ConvertFromString("ApplicationCommands.Copy");
 32:                 btnPaste.Command = (ICommand)cv.ConvertFromString("ApplicationCommands.Paste");
 33:             }
 34:         }
 35:     }
 36: }
 37: 

 

The output of this program is same as previous one.

ApplicationCommandsOutput

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: