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





5.00/5 (6 votes)
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.