私の問題は次のとおりです。
データセット内の 3 つのフィールドにバインドされた 3 つのテキスト ボックスがあります。TextBox_Rate、TextBox_Hours、TextBox_Salary。
私が必要としていたのは、TextBox_Rate + TextBox_Hoursを = TextBox_Salaryにすることでした。
これは、 MultibindingとConverterを使用することで実現できることがわかりました。
マルチバインディングは次のようになります。
<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>
そしてコンバーター:
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
クラス終了
これはすべて100%機能します。私は私が望む結果を得ています。しかし、ここでも問題が発生します...
TextBox_Rateは Dataset フィールドRateにバインドされ、TextBox_Hoursは Dataset フィールドHoursにバインドされ、元々 (マルチバインディングの前) TextBox_Salaryは Dataset フィールドSalaryにバインドされていましたが、 TextBox_Rate AND TextBox_Hoursにバインドされていません。また、Multibinding から生成された値は、ソース フィールド "Salary" にバインドされていないため、更新されません。
そのフィールドを更新するようにバインディングを設定するにはどうすればよいですか?
前もって感謝します。