views:

1049

answers:

1

So I'm creating columns dynamically for a Grid layout in Silverlight (V3.0, C#):

LayoutRoot.ColumnDefinitions.Add(new ColumnDefinition());

and you can specific that the width of the column be "auto" with

LayoutRoot.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });

But how to you set it to be "*" (share with with other columns)?

I know you can set this in the XAML, but how do you do it in code?

+11  A: 

Pass a GridLength with a GridUnitType of Star:

new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) };

Bonus is that this technique also supports proportional spacing i.e. the equivalent of XAML 2*, 3*, etc.

itowlson