How do I set DataGridColumn widths to "*" at runtime?

Apr 15, 2009 at 1:51 AM
I want to have the columns in my datagrid to all nicely resize as the surrounding grid area changes. To do so I have some code to iterate through the columns and set the widths to "*" like I would in XAML. However I can't find a struct value to set them to "*" though. DataGridLength doesn't have a * value.

private void HistoryDataGrid_Loaded(object sender, RoutedEventArgs e)
         foreach (DataGridColumn col in this.HistoryDataGrid.Columns)
            col.Width.Equals("*"); // doesn't work

Is this possible?
Apr 15, 2009 at 3:45 AM
There should be a constructor like, new DataGridLength(1.0, DataGridLengthUnitType.Star), which is equivalent to "*".
Apr 16, 2009 at 11:00 PM
Thank you.Works great.
Jul 29, 2009 at 8:41 PM
Edited Jul 29, 2009 at 8:56 PM

how can we get the computed column width when using DataGridLengthUnitType.Star (or any non-absolute datagridlength)? The ActualWidth property doesn't get updated immediately from what I can tell.

Aug 19, 2009 at 11:52 AM

Try using the ContentRendered event (I think that's what its called) for the form. At that point, ActualWidth may/should be a valid value.