2012-05-06 130 views
0

我有一个类如下:自动更新

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没有得到更新。我所做的任何理由/错误。我在很多方面尝试了很多,但无法实现这一目标。我不明白的是总金额得到更新,但最终的价格不是。谢谢。

+0

在VS2010 SP1尝试这个。只有问题是需要将Mode = OneWay添加到绑定的问题,因为TotalPrice是只读的。你使用什么版本的VS/.Net? – grantnz

+0

尝试调试不使用'PresentationTracesources.TraceLevel'工作绑定。请参阅[这里](http://bea.stollnitz.com/blog/?p=52)了解更多信息。这将帮助您查看绑定是否获取更新。如果不是做平常的断点,步进式的事情... –

+0

@ grantnz - 我使用VS2010。 @Sebastian Edelmeier,我用的断点和监视,会收到更新的值然而,问题是只能从UI。谢谢。 – surpavan

回答

0

我不记得来源,但问题是当有只读属性绑定不正常运行的时间,正在等待下一个版本的修复,我通过手动编码的文本改变事件的改变来解决。

0

请勿使用Private _FinalPriceValue在Setter TransportPriceLabourCost中指定值。使用FinalPrice适当提高PriceCalculationNotification()每次。

+0

我做了改变,那是一个表现奖金。但UI尚未更新。 – surpavan