Posted by: Zeeshan Amjad | February 1, 2010

Built in Commands


WPF has some built in commands that implement ICommand interface. These commands are divided into few groups, such as application commands, component commands, media command, navigation command and editing commands. These classes are define in System.Windows.Input namespace. Here we are going to see one application.

We can define the command binding at the top level window. Here we are defining one built in application command at grid level.

  1: <Grid.CommandBindings>
  2: 	<CommandBinding Command="ApplicationCommands.Close"
  3: 		Executed="CommandBinding_Executed" 
  4: 		CanExecute="CommandBinding_CanExecute"/>
  5: </Grid.CommandBindings>
  6: 

Here we define the handler to execute this command and when we want that button will be enable or disable. Here we want to enable the button all the time therefore we always return true in the CommandBinding_CanExecute method. Here is the implementation of these methods.

  1: private void CommandBinding_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
  2: {
  3:     Close();
  4: }
  5: 
  6: private void CommandBinding_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
  7: {
  8:     e.CanExecute = true;
  9: }
 10: 

Now we can define the command binding to specific button. Here is XAML code for this.

  1: <Button Grid.Column="5" Command="ApplicationCommands.Close"  Grid.Row="1" Width="120" Height="35">
  2: 	Exit
  3: </Button>
  4: 

Now if we click on this button then it will execute the command handler and close the application. Here we are going to use this command binding in one of our existing application. Here is complete XAML code of the program.

  1: <Window x:Class="WpfListBox.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="List Box Sample" Height="600" Width="800" Loaded="Window_Loaded">
  5:     <Grid>
  6:         <Grid.CommandBindings>
  7:             <CommandBinding Command="ApplicationCommands.Close"
  8:                             Executed="CommandBinding_Executed" 
  9:                             CanExecute="CommandBinding_CanExecute"/>
 10:         </Grid.CommandBindings>
 11:         <Grid.RowDefinitions>
 12:             <RowDefinition Height="7*"/>
 13:             <RowDefinition/>
 14:         </Grid.RowDefinitions>
 15:         <Grid.ColumnDefinitions>
 16:             <ColumnDefinition/>
 17:             <ColumnDefinition/>
 18:             <ColumnDefinition/>
 19:             <ColumnDefinition/>
 20:             <ColumnDefinition/>
 21:         </Grid.ColumnDefinitions>
 22:         
 23:         <ListBox Grid.Row="0" Grid.ColumnSpan="5" Name="lst" Margin="5" ItemsSource="{Binding}" HorizontalContentAlignment="Stretch">
 24:             <ListBox.ItemTemplate>
 25:                 <DataTemplate>
 26:                     <Border Margin="5" Background="Wheat" BorderThickness="5" BorderBrush="Brown">
 27:                         <StackPanel>
 28:                             <TextBlock Margin="5" FontSize="16" FontWeight="Bold" Text="{Binding Path=Name}"/>
 29:                             <TextBlock Margin="5, 0"  Foreground="Blue" Text="{Binding Path=Capital}"/>
 30:                             <TextBlock Margin="5, 0"  Text="{Binding Path=BigCity}"/>
 31:                             <TextBlock Margin="5" Text="{Binding Path=NickName}"/>
 32:                         </StackPanel>
 33:                     </Border>
 34:                 </DataTemplate>
 35:             </ListBox.ItemTemplate>
 36:         </ListBox>
 37:         <Button Name="sortName" Grid.Column="0" Grid.Row="1" Width="120" Height="35" Click="sortName_Click">
 38:             Sort by Name
 39:         </Button>
 40:         <Button Name="sortCapital" Grid.Column="1" Grid.Row="1" Width="120" Height="35" Click="sortCapital_Click">
 41:             Sort by Capital
 42:         </Button>
 43:         <Button Name="sortBigCity" Grid.Column="2" Grid.Row="1" Width="120" Height="35" Click="sortBigCity_Click">
 44:             Sort by Big City
 45:         </Button>
 46:         <Button Name="sortNick" Grid.Column="3" Grid.Row="1" Width="120" Height="35" Click="sortNick_Click">
 47:             Sort by Nick Name
 48:         </Button>
 49:         <Button Grid.Column="5" Command="ApplicationCommands.Close"  Grid.Row="1" Width="120" Height="35">
 50:             Exit
 51:         </Button>
 52:     </Grid>
 53: </Window>
 54: 

Here is complete  C# code of the program.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Windows;
  4: using System.Collections.ObjectModel;
  5: using System.ComponentModel;
  6: 
  7: namespace WpfListBox
  8: {
  9:     /// <summary>
 10:     /// Interaction logic for Window1.xaml
 11:     /// </summary>
 12:     public partial class Window1 : Window
 13:     {
 14:         public Window1()
 15:         {
 16:             InitializeComponent();
 17:         }
 18: 
 19:         private void Window_Loaded(object sender, RoutedEventArgs e)
 20:         {
 21:             StateInfoList states = new StateInfoList();
 22: 
 23:             states.Add(new StateInfo("Maryland", "Annapolis", "Baltimore", "Old Line State"));
 24:             states.Add(new StateInfo("California", "Sacramento", "Los Angeles", "Golden State"));
 25:             states.Add(new StateInfo("Washington", "Olympia", "Seattle", "The Evergreen State"));
 26:             states.Add(new StateInfo("Taxes", "Austin", "Houston", "Lone Star State"));            
 27:             states.Add(new StateInfo("New York", "Albany", "New York City", "Empire State"));
 28: 
 29:             lst.ItemsSource = states;
 30:         }
 31: 
 32:         private void sortName_Click(object sender, RoutedEventArgs e)
 33:         {
 34:             lst.Items.SortDescriptions.Clear();
 35:             SortDescription sd = new SortDescription("Name", ListSortDirection.Ascending);
 36:             lst.Items.SortDescriptions.Add(sd);
 37:         }
 38: 
 39:         private void sortCapital_Click(object sender, RoutedEventArgs e)
 40:         {
 41:             lst.Items.SortDescriptions.Clear();
 42:             SortDescription sd = new SortDescription("Capital", ListSortDirection.Ascending);
 43:             lst.Items.SortDescriptions.Add(sd);
 44:         }
 45: 
 46:         private void sortBigCity_Click(object sender, RoutedEventArgs e)
 47:         {
 48:             lst.Items.SortDescriptions.Clear();
 49:             SortDescription sd = new SortDescription("BigCity", ListSortDirection.Ascending);
 50:             lst.Items.SortDescriptions.Add(sd);
 51:         }
 52: 
 53:         private void sortNick_Click(object sender, RoutedEventArgs e)
 54:         {
 55:             lst.Items.SortDescriptions.Clear();
 56:             SortDescription sd = new SortDescription("NickName", ListSortDirection.Ascending);
 57:             lst.Items.SortDescriptions.Add(sd);
 58:         }
 59: 
 60:         private void CommandBinding_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
 61:         {
 62:             Close();
 63:         }
 64: 
 65:         private void CommandBinding_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
 66:         {
 67:             e.CanExecute = true;
 68:         }
 69:     }
 70: 
 71:     public class StateInfo
 72:     {
 73:         public StateInfo(string name, string capital, string bigcity, string nickname)
 74:         {
 75:             Name = name;
 76:             Capital = capital;
 77:             BigCity = bigcity;
 78:             NickName = nickname;
 79:         }
 80: 
 81:         public string Name
 82:         { get; set; }
 83: 
 84:         public string Capital
 85:         { get; set; }
 86: 
 87:         public string BigCity
 88:         { get; set; }
 89: 
 90:         public string NickName
 91:         { get; set; }
 92:     }
 93: 
 94:     public class StateInfoList : ObservableCollection<StateInfo>
 95:     {
 96:     }
 97: }
 98: 

This is the output of the program.

ApplicationBindingOutput

Advertisements

Responses

  1. […] Binding with Built in Commands We just saw the example of build in commands. We already studied about self binding. Now we are going to use these two concepts together and see […]

  2. […] in commands using command target We have already seen one example of command binding here. In that example we wrote some handler to execute the built in commands. Now we are going one step […]


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: