StackGrid usage example

by Geert 22. March 2011 20:07

Catel is an open-source MVVM Framework for WPF, Silverlight and WP7. However, it is more than that. It also contains lots of very useful controls that can be used without the MVVM Framework. This article will explain the StackGrid which ships with Catel.

The screenshot below shows a very simple entry form. A Grid is normally used to create such an entry form.

Screenshot

However, the downside of the Grid is that it requires you to set the attached Grid.Row and Grid.Column properties. Most of the entry forms are very standard, and defining the attached properties is unnecessary (if it weren’t for the Grid to require it).

The StackGrid is here to save the day. The entry form above can be written using the code below:

   1: <Controls:StackGrid>
   2:     <!-- Row definitions -->
   3:     <Controls:StackGrid.RowDefinitions>
   4:         <RowDefinition Height="Auto" />
   5:         <RowDefinition Height="Auto" />
   6:         <RowDefinition Height="Auto" />
   7:         <RowDefinition Height="Auto" MinHeight="15" />
   8:         <RowDefinition Height="Auto" />
   9:     </Controls:StackGrid.RowDefinitions>
  10:      
  11:     <!-- Column definitions -->
  12:     <Controls:StackGrid.ColumnDefinitions>
  13:         <ColumnDefinition Width="Auto" />
  14:         <ColumnDefinition Width="*" MinWidth="300" />
  15:     </Controls:StackGrid.ColumnDefinitions>
  16:      
  17:     <!-- First name, will be set to row 0, column 1 and 2 -->
  18:     <Label Content="First name" />
  19:     <TextBox Text="Geert" />
  20:      
  21:     <!-- Middle name, will be set to row 1, column 1 and 2 -->
  22:     <Label Content="Middle name" />
  23:     <TextBox Text="van" />
  24:  
  25:     <!-- Last name, will be set to row 2, column 1 and 2 -->
  26:     <Label Content="Last name" />
  27:     <TextBox Text="Horrik" />
  28:  
  29:     <!-- Empty row -->
  30:     <Controls:EmptyRow />
  31:      
  32:     <!-- Wrappanel, will span 2 columns -->
  33:     <WrapPanel Grid.ColumnSpan="2" Style="{StaticResource RightAlignedButtonsWrapPanelStyle}">
  34:         <Button Content="OK" Style="{StaticResource RightAlignedFixedSizeButtonStyle}" />
  35:         <Button Content="Cancel" Style="{StaticResource RightAlignedFixedSizeButtonStyle}" />
  36:     </WrapPanel>
  37: </Controls:StackGrid>

As you can see, with the StackGrid it is no longer required to use the Grid.Row and Grid.Column attached properties. The StackGrid uses the same technique as the Grid, so you can still define rows and columns. Then, it stacks all the controls from left to right, then top to bottom. A few advantages are:

  • Lesser code to write (time)
  • Lesser code to read (easier code reading)
  • Inserting a row is now very easy

In the example above, if you need to add a new item between the first and middle name, you would have to re-assign all row attached properties for all controls inside the grid after the inserted row. With the StackGrid, you only have to add a new row definition and insert the controls. It’s as easy as that!

The attached example application shows the code, and you can easily adjust it to see what happens.

stackgrid.zip (1.82 mb) [Downloads: 287]

Tags: ,

C# | Silverlight | WPF

WPF ComboBoxEx

by Geert 22. October 2009 10:33

Some time ago, I needed a combobox in WPF to support an empty value (null), so the user was able to deselect or undo a selection. Unfortunately, this was not supported by the standard ComboBox class WPF offers.

Therefore, I decided to create my own combobox, with a “lot” of additional features that I think a combobox must support for a developer to be really useful. The attached ComboBoxEx class is the result, and it adds the following features to the standard ComboBox class:

  • Automatically select the value if only one item is available in the items source;
  • Support the MaxLength property when IsEditable property is enabled;
  • Keep selected value selected when updating the items source;
  • Disable the control when no items are in the items source;
  • Support an empty item, which can be null, but also a default value that can be specified.

A full demo application of all features is included in the package.

comboboxex

comboboxex.zip (34.12 kb) [Downloads: 928]

kick it on DotNetKicks.com

An advanced combobox for WPF that supports empty items, automatic item selection, maxlength property and more...

Tags:

C# | WPF


About the Author

Geert van Horrik is an independent freelance software developer since January 1st, 2007. Since then he was been working on several projects from C++ to C# (WPF, Silverlight, ASP.NET, etc). Currently he loves to write his software using WPF (or Silverlight if WPF isn't an option).

Lately, Geert is spending a lot of time on Catel, a free open-source MVVM Framework for WPF and Silverlight. Actually, it's more than "just" an MVVM Framework, it's a complete application library!