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.