Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 625
Handleman Avatar asked Oct 18 '09 23:10

Handleman


1 Answers

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.

like image 125
itowlson Avatar answered Oct 23 '22 13:10

itowlson