>我有一个类如下:
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没有得到更新。 我做过的任何原因/错误。 我已经尝试了很多方法,但无法正常工作。 我不明白的是总金额正在更新,但最终价格没有。 谢谢。
不要使用 Private _FinalPriceValue
在 Setter TransportPrice
和 LabourCost
中分配值。每次都使用FinalPrice
正确提高PriceCalculationNotification()
。
我不
记得来源了,但关键是当有只读属性时,绑定有时无法正常工作并正在等待下一个版本中的修复,我通过手动编码文本更改事件解决了更改。