dotnet / dotnet/wpf

Supporting measurement units in WPF binding

Open
#7,407 2 comments 0 reactions 0 assignees View on GitHub
Enhancement Requested
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

If I want to support double type editing (for example "2.3") with unit type information ("meters", "feets") - then
one approach how to add such support could be to use MultiBinding with unit information encoded as it's own Binding path, for example:

```


```

And then within a conversion factory you have get conversion units, for example like this:

```
public class DoubleWithUnitTypeConverter : IMultiValueConverter
{
string units;

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
units = values[1] as string;

... do something with units ...

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
... use units here ...
```

This works in most of cases, with exception when ConvertBack is used on different property with different measurement unit - then units is not valid in there, as it's static, but should not be.

One approach is to use ConverterParameter,

```

```

However - that does not supports `Binding` (see [stackoverflow](https://stackoverflow.com/questions/15309008/binding-converterparameter)).

I'm wondering why `Convert` takes as an input `object[] values`, while `ConvertBack` does not do that one.

Can you recommend like official WPF way of solving this measurement unit issue ?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.