We are possibly about to introduce UnitsNet in our application for two reasons: Type safe computations and ability to switch between different unit systems (metric, imperial etc.) This means those types will be all over our application and also in our view models for presentation. And this is where it gets tricky. I would like the various quantities to be displayed more or less automatically in the GUI, solely by specifying the desired unit.
Since there will be a lot of those quantities it would be tiresome to always specify a type converter in the xaml file, and I'm not sure it would be possible to specify which unit to display at the same time.
So what would be a good approach to this? Writing your own user controls, using conversion attributes, or is there a better way?
UnitsNetquantity classes (e.g.Length,Mass, etc) have apublic staticToStringDefaultUnitproperty which you can set to whicheverUnityou want the quantity classes to be displayed as (ToString) during runtime.For example:
If you need different units per quantity then it would be a good idea to create new types to represent the difference. It would also be a good idea to copy how UnitsNet does the implementation to be consistent.
One way to leverage the
ToStringDefaultUnitproperty is to have one class that will manage the conversion for the whole app. This way, your unit conversions will not be all over the place.