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が更新されていないことです。私がした理由/間違い。私はいくつかの方法でそれをたくさん試しましたが、それを機能させることができませんでした。私が理解していなかったのは、合計金額が更新されているが、最終価格は更新されていないということです。ありがとうございました。

4

2 に答える 2

0

SetterおよびPrivate _FinalPriceValueで値を割り当てるために使用しないでください。毎回きちんと上げるために使用します。TransportPriceLabourCostFinalPricePriceCalculationNotification()

于 2012-05-06T23:52:33.880 に答える
0

ソースを覚えていませんが、重要なのは、読み取り専用プロパティがある場合、バインディングが正しく機能せず、次のリリースでの修正を待っていることです。テキスト変更イベントによる変更を手動でコーディングすることで解決しました。

于 2012-05-19T19:18:40.393 に答える