65.9K
CodeProject is changing. Read more.
Home

Setting non-numeric grid row/column sizes in WPF/Silverlight

starIconstarIconstarIconstarIconstarIcon

5.00/5 (6 votes)

Apr 13, 2010

CPOL
viewsIcon

12652

John, here's a simpler way to do this. This will also handle strings like "3*" which are valid grid length values in Xaml.private RowDefinition MakeRowDefinition(string height){ RowDefinition rowDef = new RowDefinition(); GridLengthConverter converter = new...

John, here's a simpler way to do this. This will also handle strings like "3*" which are valid grid length values in Xaml.
private RowDefinition MakeRowDefinition(string height)
{
    RowDefinition rowDef = new RowDefinition();

    GridLengthConverter converter = new GridLengthConverter();

    GridLength gridLength;

    try
    {
        gridLength = (GridLength)converter.ConvertFromString(height);
    }
    catch (FormatException)
    {
        gridLength = new GridLength();
    }

    rowDef.Height = gridLength;

    return rowDef;
}
You could also make the GridLengthConverter an instance field so you don't have to instantiate it for each call. Note : GridLengthConverter is only available in WPF. It's not available for Silverlight.