What coincidence, I had to do this earlier today. The qualified doubles end up going through a factor conversion based on the unit you give it, but as part of LengthConverter
.
LengthConverter lc = new LengthConverter();
string qualifiedDouble = "10pt";
double converted = lc.ConvertFrom( qualifiedDouble );
Alternate:
double original = 10.0;
double converted = original * 1.333333333; // px-to-pt conversion
This will transform "10pt" to 13.3333333, for example. You could also use the conversion factors the article supplies, but I prefer to use the above since the factors are built into the class.
Edited: In response to comment about strings...
String conversion makes perfect sense for what it was intended for. They use XAML because it is so much easier to express some things in XAML than in C# or VB. In XAML, all the values are strings, so you have TypeConverter
s automatically selected to convert the string to the target type. FontSizeConverter
for example, calls an internal static method on LengthConverter
. (You could also instantiate FontSizeConverter
instead.) There are also converters for GridLength
s like "4*" and Width
s like "Auto". Or, like I said, you can create your own class to convert without strings.
This article recommends, for code-behind, to use the factor directly, so I supplied an alternate example above.