Posted by: Zeeshan Amjad | February 10, 2010

Using GridLengthConverter in Code


We have already several example of defining grid in XAML. In XAML we can set the different height of columns or different width of columns. We define this in the form of pixel or even in the form of “n*” where n is a number. It is important to note that we can’t specify the height or width in the form of percentage. This looks like a limitation, but on the other hand there is one advantage of it. The advantage is now we don’t have to worry about the sum of percentage which should be 100.

Now lets going to do the same thing with code using type converter. For this purpose WPF define one converter name GridLengthConverter. Here is a class diagram of it.

GridLengthConverter

Now lets see the example of it. Here is a piece of code to use GridLengthConverter.

  1: GridLengthConverter glc = TypeDescriptor.GetConverter(typeof(GridLength)) as GridLengthConverter;
  2: 
  3: RowDefinition row1 = new RowDefinition();
  4: RowDefinition row2 = new RowDefinition();
  5: RowDefinition row3 = new RowDefinition();
  6: 
  7: if (glc != null)
  8: {                
  9:     row1.Height = (GridLength)glc.ConvertFromString("3*");                
 10:     row2.Height = (GridLength)glc.ConvertFromString("2*");
 11:     row3.Height = (GridLength)glc.ConvertFromString("4*");                
 12: }
 13: 

In this way we can use “2*, “3* etc in the code without specify the width in pixel. Here is a complete code of our program. We wrote the complete program in code without any XAML. We just define some shapes in in different columns and rows of grid.

  1: using System;
  2: using System.Windows;
  3: using System.Windows.Controls;
  4: using System.Windows.Media;
  5: using System.Windows.Input;
  6: using System.Windows.Data;
  7: using System.Windows.Shapes;
  8: using System.ComponentModel;
  9: 
 10: namespace WpfCommand
 11: {
 12:     public class MyWindow : Window
 13:     {
 14:         public MyWindow()
 15:         {
 16:             Title = "Grid Length Converter";
 17:             Height = 300;
 18:             Width = 300;
 19:             Background = Brushes.AliceBlue;
 20: 
 21:             Grid grid = new Grid();
 22: 
 23:             GridLengthConverter glc = TypeDescriptor.GetConverter(typeof(GridLength)) as GridLengthConverter;
 24: 
 25:             RowDefinition row1 = new RowDefinition();
 26:             RowDefinition row2 = new RowDefinition();
 27:             RowDefinition row3 = new RowDefinition();
 28: 
 29:             if (glc != null)
 30:             {                
 31:                 row1.Height = (GridLength)glc.ConvertFromString("3*");                
 32:                 row2.Height = (GridLength)glc.ConvertFromString("2*");
 33:                 row3.Height = (GridLength)glc.ConvertFromString("4*");                
 34:             }
 35: 
 36:             grid.RowDefinitions.Add(row1);
 37:             grid.RowDefinitions.Add(row2);
 38:             grid.RowDefinitions.Add(row3);
 39: 
 40:             ColumnDefinition col1 = new ColumnDefinition();
 41:             ColumnDefinition col2 = new ColumnDefinition();
 42: 
 43:             grid.ColumnDefinitions.Add(col1);
 44:             grid.ColumnDefinitions.Add(col2);
 45: 
 46:             Rectangle rect1 = new Rectangle();
 47:             rect1.Fill = Brushes.Blue;
 48:             rect1.Margin = (Thickness)TypeDescriptor.GetConverter(typeof(Thickness)).ConvertFromString("5");
 49: 
 50:             Grid.SetColumn(rect1, 0);
 51:             Grid.SetColumnSpan(rect1, 2);
 52:             Grid.SetRow(rect1, 0);
 53: 
 54:             Rectangle rect2 = new Rectangle();
 55:             rect2.Fill = Brushes.Green;
 56:             rect2.Margin = (Thickness)TypeDescriptor.GetConverter(typeof(Thickness)).ConvertFromString("5");
 57: 
 58:             Grid.SetColumn(rect2, 0);
 59:             Grid.SetRow(rect2, 1);
 60: 
 61:             Rectangle rect3 = new Rectangle();
 62:             rect3.Fill = Brushes.Red;
 63:             rect3.Margin = (Thickness)TypeDescriptor.GetConverter(typeof(Thickness)).ConvertFromString("5");
 64: 
 65:             Grid.SetColumn(rect3, 1);
 66:             Grid.SetRow(rect3, 1);
 67: 
 68:             Ellipse ellipse1 = new Ellipse();
 69:             ellipse1.Fill = Brushes.Wheat;
 70:             ellipse1.Margin = (Thickness)TypeDescriptor.GetConverter(typeof(Thickness)).ConvertFromString("5");
 71: 
 72:             Grid.SetColumn(ellipse1, 0);
 73:             Grid.SetRow(ellipse1, 2);
 74: 
 75:             Ellipse ellipse2 = new Ellipse();
 76:             ellipse2.Fill = Brushes.Yellow;
 77:             ellipse2.Margin = (Thickness)TypeDescriptor.GetConverter(typeof(Thickness)).ConvertFromString("5");
 78: 
 79:             Grid.SetColumn(ellipse2, 1);
 80:             Grid.SetRow(ellipse2, 2);
 81: 
 82:             grid.Children.Add(rect1);
 83:             grid.Children.Add(rect2);
 84:             grid.Children.Add(rect3);
 85:             grid.Children.Add(ellipse1);
 86:             grid.Children.Add(ellipse2);
 87: 
 88:             Content = grid;
 89:         }
 90:     }
 91: 
 92:     public class MyApplication
 93:     {
 94:         [STAThread]
 95:         public static void Main()
 96:         {
 97:             MyWindow win = new MyWindow();
 98:             Application app = new Application();
 99: 
100:             app.Run(win);
101:         }
102:     }
103: }
104: 

This is the output of the program.

GridLengthConverterOutput

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: