Posted by: Zeeshan Amjad | March 11, 2010

Using Radio Button in Status Bar Revisited


We just saw one example of using Radio button in status bar here. Now we are going to see the same example but with our own created routed command. We already created one example of creating routed command here. Here is our class of routed command.

  1: public class MyCommands
  2: {
  3:     private static RoutedCommand redCommand;
  4:     private static RoutedCommand blueCommand;
  5:     private static RoutedCommand greenCommand;
  6:     private static RoutedCommand yellowCommand;
  7: 
  8:     static MyCommands()
  9:     {
 10:         InputGestureCollection inputRed = new InputGestureCollection();
 11:         inputRed.Add(new KeyGesture(Key.R, ModifierKeys.Alt, "Alt+R"));
 12:         redCommand = new RoutedCommand("RedCommand", typeof(MyCommands), inputRed);
 13: 
 14:         InputGestureCollection inputBlue = new InputGestureCollection();
 15:         inputBlue.Add(new KeyGesture(Key.B, ModifierKeys.Alt, "Alt+B"));
 16:         blueCommand = new RoutedCommand("BlueCommand", typeof(MyCommands), inputBlue);
 17: 
 18:         InputGestureCollection inputGreen = new InputGestureCollection();
 19:         inputGreen.Add(new KeyGesture(Key.G, ModifierKeys.Alt, "Alt+G"));
 20:         greenCommand = new RoutedCommand("GreenCommand", typeof(MyCommands), inputGreen);
 21: 
 22:         InputGestureCollection inputYellow = new InputGestureCollection();
 23:         inputYellow.Add(new KeyGesture(Key.Y, ModifierKeys.Alt, "Alt+Y"));
 24:         yellowCommand = new RoutedCommand("YellowCommand", typeof(MyCommands), inputYellow);
 25:     }
 26: 
 27:     public static RoutedCommand RedCommand
 28:     {
 29:         get { return redCommand; }
 30:     }
 31: 
 32:     public static RoutedCommand BlueCommand
 33:     {
 34:         get { return blueCommand; }
 35:     }
 36: 
 37:     public static RoutedCommand GreenCommand
 38:     {
 39:         get { return greenCommand; }
 40:     }
 41: 
 42:     public static RoutedCommand YellowCommand
 43:     {
 44:         get { return yellowCommand; }
 45:     }
 46: }
 47: 

Then in XAML we define the command binding at Window level. here is XAML code to do this.

  1: <Window.CommandBindings>
  2: 	<CommandBinding Command="local:MyCommands.RedCommand" Executed="optRed_Click"/>
  3: 	<CommandBinding Command="local:MyCommands.BlueCommand" Executed="optBlue_Click"/>
  4: 	<CommandBinding Command="local:MyCommands.GreenCommand" Executed="optGreen_Click"/>
  5: 	<CommandBinding Command="local:MyCommands.YellowCommand" Executed="optYellow_Click"/>
  6: </Window.CommandBindings>
  7: 

And then do command binding of each radio  button. Here is XAML code to do the command binding with radio buttons.

  1: <RadioButton Name="optRed" Content="Red" Command="local:MyCommands.RedCommand" />
  2: <RadioButton Name="optBlue" Content="Blue" Command="local:MyCommands.BlueCommand" />
  3: <RadioButton Name="optGreen" Content="Green" Command="local:MyCommands.GreenCommand" />
  4: <RadioButton Name="optYello" Content="Yellow" Command="local:MyCommands.YellowCommand" />
  5: 

Here is complete XAML code of the program.

  1: <Window x:Class="WpfStatusBar.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:WpfStatusBar"
  5:     Title="Status Bar" Height="300" Width="300">
  6:     <Window.CommandBindings>
  7:         <CommandBinding Command="local:MyCommands.RedCommand" Executed="optRed_Click"/>
  8:         <CommandBinding Command="local:MyCommands.BlueCommand" Executed="optBlue_Click"/>
  9:         <CommandBinding Command="local:MyCommands.GreenCommand" Executed="optGreen_Click"/>
 10:         <CommandBinding Command="local:MyCommands.YellowCommand" Executed="optYellow_Click"/>
 11:     </Window.CommandBindings>
 12:     <DockPanel>
 13:         <StatusBar DockPanel.Dock="Bottom">
 14:             <RadioButton Name="optRed" Content="Red" Command="local:MyCommands.RedCommand" />
 15:             <RadioButton Name="optBlue" Content="Blue" Command="local:MyCommands.BlueCommand" />
 16:             <RadioButton Name="optGreen" Content="Green" Command="local:MyCommands.GreenCommand" />
 17:             <RadioButton Name="optYello" Content="Yellow" Command="local:MyCommands.YellowCommand" />
 18:         </StatusBar>
 19:         <Grid>
 20:             <Grid.RowDefinitions>
 21:                 <RowDefinition/>
 22:                 <RowDefinition/>
 23:             </Grid.RowDefinitions>
 24:             <Grid.ColumnDefinitions>
 25:                 <ColumnDefinition/>
 26:                 <ColumnDefinition/>
 27:             </Grid.ColumnDefinitions>
 28:             
 29:             <Border Grid.Column="0" Grid.Row="0" Margin="5" CornerRadius="10"
 30:                     Name="border1" Background="Wheat"/>
 31:             <Border Grid.Column="1" Grid.Row="0" Margin="5" CornerRadius="10"
 32:                     Name="border2" Background="Wheat"/>
 33:             <Border Grid.Column="0" Grid.Row="1" Margin="5" CornerRadius="10"
 34:                     Name="border3" Background="Wheat"/>
 35:             <Border Grid.Column="1" Grid.Row="1" Margin="5" CornerRadius="10"
 36:                     Name="border4" Background="Wheat"/>
 37:         </Grid>
 38:     </DockPanel>
 39: </Window>
 40: 

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 WpfStatusBar
 16: {
 17:     /// <summary>
 18:     /// Interaction logic for Window1.xaml
 19:     /// </summary>
 20:     public partial class Window1 : Window
 21:     {        
 22: 
 23:         public Window1()
 24:         {
 25:             InitializeComponent();
 26:         }
 27: 
 28:         private void optRed_Click(object sender, ExecutedRoutedEventArgs e)
 29:         {
 30:             border1.Background = Brushes.Red;
 31:             border2.Background = Brushes.Red;
 32:             border3.Background = Brushes.Red;
 33:             border4.Background = Brushes.Red;
 34:         }
 35: 
 36:         private void optBlue_Click(object sender, RoutedEventArgs e)
 37:         {
 38:             border1.Background = Brushes.Blue;
 39:             border2.Background = Brushes.Blue;
 40:             border3.Background = Brushes.Blue;
 41:             border4.Background = Brushes.Blue;
 42:         }
 43: 
 44:         private void optGreen_Click(object sender, RoutedEventArgs e)
 45:         {
 46:             border1.Background = Brushes.Green;
 47:             border2.Background = Brushes.Green;
 48:             border3.Background = Brushes.Green;
 49:             border4.Background = Brushes.Green;
 50:         }
 51: 
 52:         private void optYellow_Click(object sender, RoutedEventArgs e)
 53:         {
 54:             border1.Background = Brushes.Yellow;
 55:             border2.Background = Brushes.Yellow;
 56:             border3.Background = Brushes.Yellow;
 57:             border4.Background = Brushes.Yellow;
 58:         }
 59:     }
 60: 
 61:     public class MyCommands
 62:     {
 63:         private static RoutedCommand redCommand;
 64:         private static RoutedCommand blueCommand;
 65:         private static RoutedCommand greenCommand;
 66:         private static RoutedCommand yellowCommand;
 67: 
 68:         static MyCommands()
 69:         {
 70:             InputGestureCollection inputRed = new InputGestureCollection();
 71:             inputRed.Add(new KeyGesture(Key.R, ModifierKeys.Alt, "Alt+R"));
 72:             redCommand = new RoutedCommand("RedCommand", typeof(MyCommands), inputRed);
 73: 
 74:             InputGestureCollection inputBlue = new InputGestureCollection();
 75:             inputBlue.Add(new KeyGesture(Key.B, ModifierKeys.Alt, "Alt+B"));
 76:             blueCommand = new RoutedCommand("BlueCommand", typeof(MyCommands), inputBlue);
 77: 
 78:             InputGestureCollection inputGreen = new InputGestureCollection();
 79:             inputGreen.Add(new KeyGesture(Key.G, ModifierKeys.Alt, "Alt+G"));
 80:             greenCommand = new RoutedCommand("GreenCommand", typeof(MyCommands), inputGreen);
 81: 
 82:             InputGestureCollection inputYellow = new InputGestureCollection();
 83:             inputYellow.Add(new KeyGesture(Key.Y, ModifierKeys.Alt, "Alt+Y"));
 84:             yellowCommand = new RoutedCommand("YellowCommand", typeof(MyCommands), inputYellow);
 85:         }
 86: 
 87:         public static RoutedCommand RedCommand
 88:         {
 89:             get { return redCommand; }
 90:         }
 91: 
 92:         public static RoutedCommand BlueCommand
 93:         {
 94:             get { return blueCommand; }
 95:         }
 96: 
 97:         public static RoutedCommand GreenCommand
 98:         {
 99:             get { return greenCommand; }
100:         }
101: 
102:         public static RoutedCommand YellowCommand
103:         {
104:             get { return yellowCommand; }
105:         }
106:     }
107: }
108: 

The output of this program is same as previous program. When program runs this will be the output of the program.

StatusBarRadioOutput_01

And this is the output when user click on Blue radio button.

StatusBarRadioOutput_02

Advertisements

Responses

  1. […] Status Bar We already saw the example of using Radion button inside the status bar here and here. Now we are going to use Progress bar inside the status bar. The first difference we notice here is […]


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: