我有一堂课如下:
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 没有更新。我所做的任何原因/错误。我已经用几种方法尝试了很多,但无法让它发挥作用。我不明白的是,总金额正在更新,但最终价格没有。谢谢你。