Welcome to MSDN Blogs Sign in | Join | Help

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:

<UserControl 
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
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.

Published Monday, February 23, 2009 11:51 AM by Michael S. Scherotter
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Silverlight Cream for February 23, 2009 -- #526

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

Monday, February 23, 2009 7:28 PM by Community Blogs

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker