UI WPF表单中的值自动更新

时间:2012-05-06 23:36:16

标签: wpf vb.net binding bindinglist

我有一个课程如下:

Public Class BillAmounts
    Implements INotifyPropertyChanged
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Private Sub NotifyPropertyChange(ByVal info As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub
    Private _LabourCostValue As Double
    Private _TransportPriceValue As Double
    Private _ItemsTotalCost_ As Double
    Private _FinalPriceValue As Double
    Property TransportPrice As Double
        Get
            Return _TransportPriceValue
        End Get
        Set(ByVal value As Double)
            If Not _TransportPriceValue = value Then
                _TransportPriceValue = value
                _FinalPriceValue = TransportPrice + LabourCost + ItemsTotalCost_
                PriceCalculationNotification()
            End If
        End Set
    End Property
    Property LabourCost As Double
        Get
            Return _LabourCostValue
        End Get
        Set(ByVal Value As Double)
            If Not _LabourCostValue = Value Then
                _LabourCostValue = Value
                _FinalPriceValue = TransportPrice + LabourCost + ItemsTotalCost_
                PriceCalculationNotification()
            End If
        End Set
    End Property
    Property ItemsTotalCost_ As Double
        Get
            Return _ItemsTotalCost_
        End Get
        Set(ByVal value As Double)
            If Not _ItemsTotalCost_ = value Then
                _ItemsTotalCost_ = value
                FinalPrice = TransportPrice + LabourCost + ItemsTotalCost_
                'NotifyPropertyChange("ItemsTotalCost_")
                PriceCalculationNotification()
            End If
        End Set
    End Property
    ReadOnly Property TotalPrice As Double
        Get
            Try
                Return ItemsTotalCost_ + TransportPrice + LabourCost
            Catch ex As Exception
                Return 0
            End Try
        End Get
        'Set(ByVal value As Double)
        '    If Not _TotalpriceValue = value Then
        '        _TotalpriceValue = value
        '        NotifyPropertyChange("TotalPrice")
        '    End If
        'End Set
    End Property
    Property FinalPrice As Double
        Get
            Return _FinalPriceValue
        End Get
        Set(ByVal value As Double)
            If Not _FinalPriceValue = value Then
                _FinalPriceValue = value
                PriceCalculationNotification()
            End If
        End Set
    End Property
    ReadOnly Property Discount_ As Double
        Get
            '_Discount_ = FinalPrice - TotalPrice
            'Return _Discount_
            Return FinalPrice - TotalPrice
        End Get
    End Property

    Public Sub New()
        _TransportPriceValue = 0
        _LabourCostValue = 0
        _ItemsTotalCost_ = 0
        _FinalPriceValue = 0
    End Sub
    Private Sub PriceCalculationNotification()
        NotifyPropertyChange("TransportPrice")
        NotifyPropertyChange("LabourCost")
        NotifyPropertyChange("Discount_")
        NotifyPropertyChange("TotalPrice")
        NotifyPropertyChange("FinalPrice")
    End Sub

End Class

我有以下字段的界限:

<StackPanel Name="AmountStack" Orientation="Vertical" >
                <Grid Margin="5">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition Width="30"/>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>

                    <TextBlock Text="Transport"  Grid.Column="0" Grid.Row="0" VerticalAlignment="Center" />
                    <TextBox Text="{Binding TransportPrice}" Name="TransportTxtBox" Grid.Column="1" Grid.Row="0" VerticalAlignment="Center" />
                    <TextBlock Text="Labour"  Grid.Column="3" Grid.Row="0" VerticalAlignment="Center" />
                    <TextBox Text="{Binding LabourCost}" Name="labourTxtBox" Grid.Column="4" Grid.Row="0" VerticalAlignment="Center" />

                    <TextBlock Text="Total Amount =" VerticalAlignment="Center" Grid.Column="0" Grid.Row="1"/>
                    <TextBox Text="{Binding TotalPrice}" Name="TotalTextBox"  IsReadOnly="True" Grid.Column="1" Grid.Row="1" />
                    <TextBlock Text="Discount= " VerticalAlignment="Center" Grid.Column="3" Grid.Row="1"/>
                    <TextBox Text="{Binding Path=Discount_, Mode=OneWay}" IsReadOnly="True" Name="DiscountTextBox" Grid.Column="4" Grid.Row="1" />
                </Grid>
                <StackPanel  Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,5,0,0">
                    <TextBlock Text="Total Amount = " VerticalAlignment="Center" />
                    <TextBox Text="{Binding Path=FinalPrice}" Name="FinalTotalTextBox" Width="130" />
                </StackPanel>
            </StackPanel>

AmountStack.DataContext = Bill.Amounts

然而,问题是TOtalAMount会自动更新,但FinalPrice没有得到更新。我做过的任何理由/错误。我在很多方面尝试了很多但是无法正常工作。我不明白的是,总金额正在更新,但最终价格不是。谢谢。

2 个答案:

答案 0 :(得分:0)

请勿使用Private _FinalPriceValue在Setter TransportPriceLabourCost中分配值。每次正确使用FinalPrice提升PriceCalculationNotification()

答案 1 :(得分:0)

我不记得源代码,但关键是当有只读属性时绑定无法正常工作并等待下一版本中的修复,我通过手动编码文本更改事件的更改来解决。 / p>