Hello Community!
Following situation, I created a datagrid to display some settings (length, heigt, degrees) to the user. Our customer wants to be able, to show the current measurement units ([mm], [inch]). Depending if the unit is in [mm], the value should be shown with one decimal digit and the [mm] unit in the same column. If the measurement system set to [inch] the value should be displayed with two decimal digits and [inch] in the same column.
As the values are later transfered to a plc, I have to do some conversions e.g. calculate the corresponding value of inch input to [10/mm] as the datatype in the bound list is an integer and is later transfered to a PLC.
I know that I should use an multivalue converter with two values. The first value (the input value) is bound to the current DataItem of the bound list. The second value for the converter is a property on the ViewModel.
Where do I have to add the MultivalueConverter and how should I add the binding to the ViewModel Property? Are there any examples or is there a better way for unit conversions?
Thank you for the input!
Hello grubarec,
Thank you for your post!
I have been looking into it and what I can suggest is creating a style for the CellValuePresenter and bind the Value property to the MultiValueConverter.
Here is an example:
<Style x:Key="ValConvertStyle" TargetType="{x:Type igDP:CellValuePresenter}">
<Setter Property="Value" >
<Setter.Value>
<MultiBinding Converter="{StaticResource MyConverter}">
<Binding RelativeSource="{RelativeSource Self}" Path="Record.DataItem"/>
<Binding RelativeSource="{RelativeSource AncestorType=Window}" Path="DataContext.VIEWMODELPROP"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
In the above code snippet the first binding is used to bind to the value of the datasource. The second one binds to the property of the ViewModel, which is used for determining if the value should be converted do inches or mm.
Please do not hesitate to let me know if you have any further questions on this matter.
Hello Gergana!
Thank you for the Example. In my case, I found a different way for my target, as the ConvertBack method introduced other problems. The units are now stored in a separate column with binding and readonly.