You can create a custom converter which converts the string representation to double (using LengthConverter):
[ValueConversion(typeof(string), typeof(double))]
public class SizeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double size = (double)new LengthConverter().ConvertFrom(value.ToString());
return size;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException("SizeConverter is a oneway converter.")
}
}
After that you can refer that converter from your XAML:
<src:SizeConverter x:Key="sizeConverter"/>
<Canvas Height="{Binding Path=Height, Converter={StaticResource sizeConverter}}"
Width="{Binding Path=Width, Converter={StaticResource sizeConverter}}" />
(Here Height
and Width
are strings
available in the DataContext
of your Canvas
.)