Search Wiki:
Resource Page Description
Custom Attached Properties for the Silverlight 2 DataGrid. This adds a StarColumn property that can be used to make a column take up all available width.
Use the StarColumn custom attached property to make a column in the DataGrid take up the rest of the available width:
<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="GridProperties.Page"
    <Grid x:Name="LayoutRoot" Background="White">
        <data:DataGrid gp:GridEx.StarColumn="2">
                <data:DataGridTextColumn Header="Column 1"/>
                <data:DataGridTextColumn Header="Column 2"/>
                <data:DataGridTextColumn Header="Column 3"/>
Last edited Feb 23 2009 at 7:47 PM  by mscherotter, version 2
plundberg wrote  Dec 8 2009 at 7:46 PM  
Great work, Michael! One comment though: I needed to make the following change to make the DataGrid behave correctly sometimes. This is in a use case where I use Prism (Composite WPF) to make a composite application. The grid is used within one of the views of the application. Without this change, I run into a "Layout cycle detected" error. If I move the code to the top-level view (the Shell in Composite WPF talk), I don't get any layout cycle but instead I get scrollbars on the grid, which might not be optimal either.

This is with Silverlight 3 btw. Here is the updated method of mine. It's only really the -2 that has been added...

/// <summary>
/// Resize the star column when the datagrid size changes
/// </summary>
/// <param name="sender">the data grid</param>
/// <param name="e">the size changed event args</param>
private static void OnSizeChanged(object sender, SizeChangedEventArgs e)
var grid = sender as DataGrid;

int starColumn = GetStarColumn(grid);

if (starColumn >= 0)
int index = 0;
double width = 0.0;

foreach (var column in grid.Columns)
if (index != starColumn)
width += column.ActualWidth;


// The -2 part was needed for me when using this control with Silverlight 3. Without it, I ran into a "Layout
// cycle" situation (the grid width itself was growing by 2 pixels for each time the event was called...).
grid.Columns[starColumn].Width = new DataGridLength(e.NewSize.Width - width - 2);

Thanks again for your great work! :)

Best regards,

Oldman0215 wrote  Apr 6 2010 at 12:54 AM  
Even with the Change plundberg suggests I can't get this to work when the datagrid is set to auto size. When the SizeChanged event is fired it always gives a size larger than the grid actually will be rendered and so the column is adjusted too large and the grid is expanded beyond the screen width. If I remove this property that grid sizes correctly but of course the columns don't fill the grid. If I set an absolute width or a MaxWidth of the datagrid then the auto sizing of the column works.

Page view tracker