Multibinding. How to update correct value to source?

1.7k views Asked by At

My issue is as follows.

I have 3 Textboxes bound to 3 fields in a Dataset. TextBox_Rate , TextBox_Hours , TextBox_Salary .

What i needed was for TextBox_Rate + TextBox_Hours to be = TextBox_Salary .

I found out that this can be achieved by use of a Multibinding and a Converter .

The Multibinding looks as follows:

<TextBox FontSize="14.667" HorizontalAlignment="Right" HorizontalContentAlignment="Right" Style="{StaticResource TextBoxStyle}">
    <TextBox.Text>
        <MultiBinding Converter="{StaticResource SalaryConverter}" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" ValidatesOnExceptions="True" NotifyOnValidationError="True" NotifyOnSourceUpdated="True" StringFormat="C">
            <Binding Path="Rate Per Hour"/>
            <Binding Path="Hours Per Month"/>
        </MultiBinding>
    </TextBox.Text>
</TextBox>

And the Converter:

Public Class SalaryConverter
Implements IMultiValueConverter

Dim weeklyHours As Double = 0

Public Function Convert(ByVal values() As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert
    Dim salary As Decimal = 0
    If values(0).Equals(System.Windows.DependencyProperty.UnsetValue) Or values(1).Equals(System.Windows.DependencyProperty.UnsetValue) Then
        Return salary
    Else
        salary = (Math.Round(values(0) * (values(1) * 4)))
        weeklyHours = values(1)
        Return salary
    End If
End Function

Public Function ConvertBack(ByVal value As Object, ByVal targetTypes() As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack
    Dim testVal As Decimal = CType((value.ToString.Replace("R ", "").Replace(",", "") / weeklyHours), Decimal) / 4
    Return New Object() {testVal}
End Function

End Class

All this works 100%. I am getting the results which i want. But this is also where the problem comes in...

TextBox_Rate is bound to Dataset field Rate, TextBox_Hours is bound to Dataset field Hours and originally (before the multibinding) TextBox_Salary was bound to Dataset field Salary but is nou bound to TextBox_Rate AND TextBox_Hours. And the value produced from the Multibinding is not updated back to the source field "Salary" since it isnt bound that field.

How do i set the binding to update that field?

Thanks in Advance.

3

There are 3 answers

1
GaaTY On BEST ANSWER

My Solution?

I simply discarded the idea of updaitng the correct binding.

Just like in the converter, anywhere where i need the "Salary" i use the combination of the "Rate" and the "Hours".

Seems to be working.

0
belugabob On

Or, even simpler, define Salary as a read-only, calculated property of the DataSet. Then you can bind your TextBoxSalary text box to this property, dispense with your converter altogether, removing the associated business logic from your view.

0
Spook On

You can add an additional binding, eg. bind to TextBox_Hours (one way), TextBox_Rate (one way) and the dataset Salary (two way, or one way to source). Then implement the ConvertBack method of converter to provide a value to dataset and Binding.DoNothing to both textboxes.