Extending the Silverlight DataGrid Class with a Custom Attached Property

One of my customers has been using the Silverlight 2 DataGrid and came to me with a request:  How do you make a column take up all of the rest of the available space (like the Grid * column width)?  If I were doing this in MFC or Windows forms, I’d probable use inheritance to add this functionality but in Silverlight I thought about using a Custom Attached Property.  The benefit of doing this is that it the code is very self-contained, it doesn’t add complexity to the grid class, and can be put into the declarative XAML.  You can see this in the DataGrid StarColumn attribute in this DataGrid.  In this instance, the third column will resize to the rest of the grid width:

x:Class="GridProperties.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:gp="clr-namespace:GridProperties"> <Grid x:Name="LayoutRoot" Background="White"> <data:DataGrid gp:GridEx.StarColumn="2"> <data:DataGrid.Columns> <data:DataGridTextColumn Header="Column 1"/> <data:DataGridTextColumn Header="Column 2"/> <data:DataGridTextColumn Header="Column 3"/> </data:DataGrid.Columns> </data:DataGrid> </Grid> </UserControl>

Download the source code here.

Comments (1)

  1. In this issue: Fredrik Normén(2), Timmy Kokke, Shemesh, Manish Dalal, Terence Tsang, Michael S. Scherotter

Skip to main content