Posted by: Zeeshan Amjad | February 21, 2010

User Custom Command Binding with Toolbar


We already saw how to bind the build in command with tool bar here. We also saw how to create a routed command here. Now we are going to combine these two concepts and create a routed command and bind it with tool bar.

Lets firs create a routed command. Here is a code to create routed command.

  1: public class MyCommandClass
  2: {
  3:     private static RoutedCommand gcdCommand;
  4:     private static RoutedCommand lcmCommand;
  5: 
  6:     static MyCommandClass()
  7:     {
  8:         InputGestureCollection inputs1 = new InputGestureCollection();
  9:         inputs1.Add(new KeyGesture(Key.G, ModifierKeys.Alt, "Alt+G"));
 10:         gcdCommand = new RoutedCommand("GCDCommand", typeof(MyCommandClass), inputs1);
 11: 
 12:         InputGestureCollection inputs2 = new InputGestureCollection();
 13:         inputs2.Add(new KeyGesture(Key.L, ModifierKeys.Alt, "Alt+L"));
 14:         lcmCommand = new RoutedCommand("LCMCommand", typeof(MyCommandClass), inputs2);
 15:     }
 16: 
 17:     public static RoutedCommand GCDCommand
 18:     {
 19:         get
 20:         {
 21:             return gcdCommand;
 22:         }
 23:     }
 24: 
 25:     public static RoutedCommand LCMCommand
 26:     {
 27:         get
 28:         {
 29:             return lcmCommand;
 30:         }
 31:     }
 32: }
 33: 

Now we can do command binding at window level. And then bind commands with tool bar. Here is a complete XAML code of the program.

  1: <Window x:Class="WpfToolBar.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     xmlns:local="clr-namespace:WpfToolBar"
  5:     Title="Toolbar" Height="300" Width="400">
  6:     <Window.CommandBindings>
  7:         <CommandBinding Command="local:MyCommandClass.GCDCommand" Executed="GCDCommand_Executed"/>
  8:         <CommandBinding Command="local:MyCommandClass.LCMCommand" Executed="LCMCommand_Executed"/>
  9:     </Window.CommandBindings>
 10:     <DockPanel>
 11:         <ToolBar DockPanel.Dock="Top">
 12:             <Button Content="GCD" Command="local:MyCommandClass.GCDCommand"/>
 13:             <Button Content="LCM" Command="local:MyCommandClass.LCMCommand"/>           
 14:         </ToolBar>
 15:         <Grid Background="AliceBlue">
 16:             <Grid.RowDefinitions>
 17:                 <RowDefinition/>
 18:                 <RowDefinition/>
 19:                 <RowDefinition/>
 20:             </Grid.RowDefinitions>
 21:             <Grid.ColumnDefinitions>
 22:                 <ColumnDefinition/>
 23:                 <ColumnDefinition/>
 24:             </Grid.ColumnDefinitions>
 25:             <TextBlock Grid.Column="0" Grid.Row="0" Margin="5" VerticalAlignment="Center">
 26:                 Enter First Number
 27:             </TextBlock>
 28:             <TextBox Grid.Column="1" Grid.Row="0" Text="{Binding FirstNo}"
 29:                      VerticalAlignment="Center" Margin="5"/>
 30:             <TextBlock Grid.Column="0" Grid.Row="1" Margin="5" VerticalAlignment="Center">
 31:                 Enter Second Number
 32:             </TextBlock>
 33:             <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding SecondNo}" 
 34:                      VerticalAlignment="Center" Margin="5"/>
 35:             <TextBlock Grid.Column="0" Grid.Row="2" Margin="5" VerticalAlignment="Center">
 36:                 Result
 37:             </TextBlock>
 38:             <TextBlock Grid.Column="1" Grid.Row="3" Text="{Binding Result}"
 39:                        Margin="5" VerticalAlignment="Center"/>
 40:         </Grid>
 41:     </DockPanel>
 42: </Window>
 43: 

And 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: 
 15: namespace WpfToolBar
 16: {
 17:     /// <summary>
 18:     /// Interaction logic for Window1.xaml
 19:     /// </summary>
 20:     public partial class Window1 : Window
 21:     {
 22:         private Numbers numbers = new Numbers();
 23: 
 24:         public Window1()
 25:         {
 26:             InitializeComponent();
 27: 
 28:             DataContext = numbers;
 29:         }
 30: 
 31:         private void GCDCommand_Executed(object sender, ExecutedRoutedEventArgs e)
 32:         {
 33:             numbers.Result = CalculateGCDInternal(numbers.FirstNo, numbers.SecondNo);
 34:         }
 35: 
 36:         private void LCMCommand_Executed(object sender, ExecutedRoutedEventArgs e)
 37:         {
 38:             numbers.Result = CalculateLCMInternal(numbers.FirstNo, numbers.SecondNo);
 39:         }
 40: 
 41:         private int CalculateGCDInternal(int x, int y)
 42:         {
 43:             if (x == 0)
 44:                 return 0;
 45: 
 46:             while (y != 0)
 47:             {
 48:                 if (x > y)
 49:                     x = x - y;
 50:                 else
 51:                     y = y - x;
 52:             }
 53: 
 54:             return x;
 55:         }
 56: 
 57:         private int CalculateLCMInternal(int x, int y)
 58:         {
 59:             int lcm = x * y / CalculateGCDInternal(x, y);
 60: 
 61:             return lcm;
 62:         }
 63:     }
 64: 
 65:     public class Numbers : DependencyObject
 66:     {
 67:         public static readonly DependencyProperty FirstNoProperty =
 68:             DependencyProperty.Register("FirstNo", typeof(int), typeof(Numbers));
 69: 
 70:         public static readonly DependencyProperty SecondNoProperty =
 71:             DependencyProperty.Register("SecondNo", typeof(int), typeof(Numbers));
 72: 
 73:         public static readonly DependencyProperty ResultProperty =
 74:             DependencyProperty.Register("Result", typeof(int), typeof(Numbers));
 75: 
 76:         public static readonly DependencyProperty ResultLabelProperty =
 77:             DependencyProperty.Register("ResultLabel", typeof(String), typeof(Numbers));
 78: 
 79:         public int FirstNo
 80:         {
 81:             get { return (int)GetValue(FirstNoProperty); }
 82:             set { SetValue(FirstNoProperty, value); }
 83:         }
 84: 
 85:         public int SecondNo
 86:         {
 87:             get { return (int)GetValue(SecondNoProperty); }
 88:             set { SetValue(SecondNoProperty, value); }
 89:         }
 90: 
 91:         public String ResultLabel
 92:         {
 93:             get { return (String)GetValue(ResultLabelProperty); }
 94:             set { SetValue(ResultLabelProperty, value); }
 95:         }
 96: 
 97:         public int Result
 98:         {
 99:             get { return (int)GetValue(ResultProperty); }
100:             set { SetValue(ResultProperty, value); }
101:         }
102:     }
103: 
104:     public class MyCommandClass
105:     {
106:         private static RoutedCommand gcdCommand;
107:         private static RoutedCommand lcmCommand;
108: 
109:         static MyCommandClass()
110:         {
111:             InputGestureCollection inputs1 = new InputGestureCollection();
112:             inputs1.Add(new KeyGesture(Key.G, ModifierKeys.Alt, "Alt+G"));
113:             gcdCommand = new RoutedCommand("GCDCommand", typeof(MyCommandClass), inputs1);
114: 
115:             InputGestureCollection inputs2 = new InputGestureCollection();
116:             inputs2.Add(new KeyGesture(Key.L, ModifierKeys.Alt, "Alt+L"));
117:             lcmCommand = new RoutedCommand("LCMCommand", typeof(MyCommandClass), inputs2);
118:         }
119: 
120:         public static RoutedCommand GCDCommand
121:         {
122:             get
123:             {
124:                 return gcdCommand;
125:             }
126:         }
127: 
128:         public static RoutedCommand LCMCommand
129:         {
130:             get
131:             {
132:                 return lcmCommand;
133:             }
134:         }
135:     }
136: }
137: 

This is the output of the program.

ToolBarCustomCommandBinding

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: