Posted by: Zeeshan Amjad | August 2, 2009

Primitives Panels in WPF: UniformGrid


Panel class is a based class of all of the layout schemes. In addition if we want to create our own layout then we have to inherit our class from the Penel class.

WPF already provides some layout for us. Here is a class diagram of all the classes inherited by the Panel class.

Panel_01

There are two types of layout classes inherited from Panel class. One is normal layout classes such as Canvas, DockPanel, Grid, StackPanel, WrapPanel etc and other is primitive panel for example ToolBarPanel, TabPanel, UniformGrid etc. These classes are defined in different namespaces. Here is the diagram of these namespaces.

Panel_02

Here is little bit modified class diagram of Panel’s inherited classes to show the namespace where these classes exists.

Panel_03

Lets take a look at UniformGrid class. It is very similar to Grid class, the only difference is it has the same width for all the columns and same height for all the rows. In other words, with UniformGrid it is not possible to create row or column with different width or heights. This layout is useful when we want to make the rows or columns with the same width or height.

Here is one simple C# program to create a chess board.

  1: using System;
  2: using System.Windows;
  3: using System.Windows.Media;
  4: using System.Windows.Controls;
  5: using System.Windows.Controls.Primitives;
  6: 
  7: public class MyWindow : Window
  8: {
  9:     private readonly int rows = 8;
 10:     private readonly int cols = 8;
 11: 
 12:     public MyWindow()
 13:     {
 14:         Title = "UniformGrid Demo";
 15:         WindowStartupLocation = WindowStartupLocation.CenterScreen;
 16:         Width = 400;
 17:         Height = 400;
 18: 
 19:         UniformGrid ug = new UniformGrid();
 20:         Border[,] border = new Border[rows, cols];
 21: 
 22:         for (int row = 0; row < rows; row++)
 23:         {
 24:             for (int col = 0; col < cols; col++)
 25:             {
 26:                 border[row, col] = new Border();                
 27: 
 28:                 if ((row + col) % 2 == 0)
 29:                 {
 30:                     border[row, col].Background = Brushes.Black;
 31:                 }
 32: 
 33:                 ug.Children.Add(border[row, col]);
 34:             }
 35:         }
 36: 
 37:         Content = ug;        
 38:     }
 39: }
 40: 
 41: public class UniformGridDemo
 42: {
 43:     [STAThread]
 44:     public static void Main()
 45:     {
 46:         MyWindow win = new MyWindow();
 47: 
 48:         Application app = new Application();
 49:         app.Run(win);
 50:     }
 51: }
 52: 

Here is the output of the program.

UniformGrid

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: