Posted by: Zeeshan Amjad | February 2, 2010

Self 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 one more advantage of self binding. WPF define some commands in two namespaces. Here is a block diagram to show namespaces.

WpfCommands

We can display the name of the command when doing command binding with button. Here is a piece of XAML code to demonstrate this.

  1: <Button Grid.Column="5" Command="ApplicationCommands.Close"  Grid.Row="1" Width="120" Height="35"
  2: 	Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"/>
  3: 

 

Now we don’t need to specify the text on the button. It will display the text of the ApplicationCommand’s Text property. 

Now we are going to change our existing sorting program and add the self binding of WPF built in commands.

Here is a complete XAML code of our 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:             Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"/>
 51:     </Grid>
 52: </Window>
 53: 

 

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 this program.

SelfBindingWithCommandBinding

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: