views:

59

answers:

1

We have an application that will be reading real world dimensions from the DB in milimetres, these values will then be presented to the user in WPF. Currently I'm using the following strategy when reading objects from the data store by multiplying MilimetresPrWpfUnit in the following example.

public const double MilimetresPerInch = 25.4;
public const double WpfUnitsPerInch = 96;
public const double MilimetresPerWpfUnit
    = WpfUnitsPerInch / MilimetresPerInch;

When loading and creating objects I'm currently converting as objects are being created, however this poses the problem that when the user selects an object on screen the size of that object needs to be converted back to the real world size, this isn't much of a problem but there is a lot of conversion going on.

Is there any utility in WPF to handle changing the unit size to metric? That might be the best solution.

The alternative is to create an IValueConverter that will convert from/to metric units, though this I fear could have a performance impact.

+1  A: 

Hey Brett, is your concern that there will be a performance hit for the conversion if you do it yourself? I dunno but if you think about it, whether this is automated by the framework or done by yourself, there will always be a performance hit for conversion. You really just need to work out at which point your data can be converted so that the conversion does not need to be replicated continually as you work with the data.

A few ways to approach this...

The IValueConverter method would definately be one way of tackling this, or you could write a method that converts the data to the metric you want when the data is being loaded, but all in all something like this is really a design specific scenario and something where one would need to see more of the code or get a more general explanation of the algorithm to make any proper recommendations.

Mark Pearl
Thanks Mark, you outlined my concerns and thoughts also. My main concern is performance. In our current prototype I'm using an IValueConverter which is working nicely binding to an XML data source. This currently works well as the XML data is stored in `mm` and dimensions are fixed. The problem will start to see performance issues when the dimensions of objects are allowed to change, this would mean for every change there will be a conversion. The thought is that we will convert to objects that represent the WPF unit size rather than the real world size. Serialisation will then convert back.
Brett Ryan
One concern we do have is that users need to see what the real world dimension of these objects are, so no matter what I can't really get away from conversion; however I will not be using `IValueConverter` as this requires unnecessary casting and type checking, I'll be just using a read-only property that performs the conversion.
Brett Ryan