Handleman
Handleman

Reputation: 754

How do I set a Silverlight Grid layout ColumnDefinition width to "*" programatically?

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?

Upvotes: 9

Views: 6611

Answers (1)

itowlson
itowlson

Reputation: 74832

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.

Upvotes: 20

Related Questions