多重绑定。如何将正确的值更新到源?

发布于 2024-10-17 23:21:37 字数 2467 浏览 8 评论 0原文

我的问题如下。

我有 3 个文本框绑定到数据集中的 3 个字段。 TextBox_RateTextBox_HoursTextBox_Salary

我需要的是 TextBox_Rate + TextBox_Hours = TextBox_Salary

我发现这可以通过使用 多重绑定转换器 来实现。

多重绑定如下所示:

<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

End Class

所有这些都 100% 有效。我正在得到我想要的结果。但这也是问题所在...

TextBox_Rate 绑定到数据集字段 RateTextBox_Hours 绑定到数据集字段 Hours 和最初(在多重绑定之前)TextBox_Salary 绑定到数据集字段 Salary,但现在绑定到 TextBox_Rate AND TextBox_Hours。并且从多重绑定生成的值不会更新回源字段“Salary”,因为它未绑定该字段。

如何设置绑定以更新该字段?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

负佳期 2024-10-24 23:21:37

我的解决方案?

我只是放弃了更新正确绑定的想法。

就像在转换器中一样,在我需要“薪水”的任何地方,我都会使用“费率”和“工时”的组合。

似乎正在工作。

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.

空城缀染半城烟沙 2024-10-24 23:21:37

您可以添加额外的绑定,例如。绑定到 TextBox_Hours(一种方式)、TextBox_Rate(一种方式)和数据集 Salary(两种方式,或一种源方式)。然后实现转换器的 ConvertBack 方法,为数据集提供值,并为两个文本框提供 Binding.DoNothing。

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.

棒棒糖 2024-10-24 23:21:37

或者,更简单的是,将 Salary 定义为 DataSet 的只读计算属性。
然后,您可以将 TextBoxSalary 文本框绑定到此属性,完全放弃转换器,从视图中删除关联的业务逻辑。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文