Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WPF

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

5.00/5 (6 votes)
14 Apr 2010CPOL 1  
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.

C#
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)