UI WPF表单中值的自动更新
我的课程如下:UI WPF表单中值的自动更新,wpf,vb.net,binding,bindinglist,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)
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>
我将字段绑定如下:
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>
然而,问题是TOtalAMount会自动更新,但FinalPrice不会更新。我做过的任何原因/错误。我已经尝试了很多方法,但都没有成功。我不明白的是,总金额正在更新,但最终价格没有更新。谢谢。请勿使用私人最终价格评估值在Setter运输价格和人工成本中赋值。每次正确使用FinalPrice提高PriceCalculationNotification。我不记得源代码,但问题是当存在只读属性时,绑定有时无法正常工作,并等待下一版本的修复,我通过手动编写文本更改事件更改代码来解决。我做了更改,这是绩效奖金。但UI尚未更新。请在VS2010 SP1上尝试此操作。唯一的问题是需要将Mode=one添加到绑定中的绑定,因为TotalPrice是只读的。您使用的是什么版本的VS/.Net?请尝试使用PresentationTracesources.TraceLevel调试不起作用的绑定。有关更多信息,请参阅。这将帮助您查看绑定是否得到更新。如果不使用通常的断点,请单步执行…@grantnz-我使用的是VS2010@Sebastian Edelmeier,我使用了断点和手表,更新了值,但问题只是来自UI。非常感谢。