使用ObservableCollection进行WPF数据绑定

使用ObservableCollection进行WPF数据绑定,wpf,binding,observablecollection,inotifypropertychanged,Wpf,Binding,Observablecollection,Inotifypropertychanged,在WPF窗口上,我有一个简单的十进制值列表框,它有一个绑定到它的金额的可观察集合,一个绑定到Total属性的标签,显示列表框下面的值的总和,还有一个绑定到selectedItem.Amount属性的列表框右边的文本框 当我单击列表框中的某个项目时,我希望能够编辑填充的文本框中selectedItem的值,从文本框中选择tab,并让listBoxItem更新其值,同时我希望标签中的总和也能更新 我了解元素到元素数据绑定的工作原理(即列表框到文本框) 我很难弄清楚的是元素到对象的数据绑定(即List

在WPF窗口上,我有一个简单的十进制值列表框,它有一个绑定到它的金额的可观察集合,一个绑定到Total属性的标签,显示列表框下面的值的总和,还有一个绑定到selectedItem.Amount属性的列表框右边的文本框

当我单击列表框中的某个项目时,我希望能够编辑填充的文本框中selectedItem的值,从文本框中选择tab,并让listBoxItem更新其值,同时我希望标签中的总和也能更新

我了解元素到元素数据绑定的工作原理(即列表框到文本框) 我很难弄清楚的是元素到对象的数据绑定(即ListBox/ObservableCollection到Total属性)

非常感谢

以下是我迄今为止的两个简单类:

Public Class TransactionModel
Implements INotifyPropertyChanged
'Public Property Amount As Decimal
Private _amount As Decimal
Public Property Amount As Decimal
    Get
        Return _amount
    End Get
    Set(ByVal value As Decimal)
        _amount = value
        OnPropertyChanged(New PropertyChangedEventArgs("Amount"))
    End Set
End Property

Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
    If Not e Is Nothing Then
        RaiseEvent PropertyChanged(Me, e)
    End If
End Sub
末级

公共类视图模型 实现INotifyPropertyChanged

Private oc As ObservableCollection(Of TransactionModel)
Sub New()
    oc = New ObservableCollection(Of TransactionModel)
    oc.Add(New TransactionModel With {.Amount = 10.0})
    oc.Add(New TransactionModel With {.Amount = 20.0})
    oc.Add(New TransactionModel With {.Amount = 30.0})
    oc.Add(New TransactionModel With {.Amount = 40.0})
End Sub

Public Function GetAmounts() As ObservableCollection(Of TransactionModel)
    Return oc
End Function

Private _total As Decimal = 0.0
Public Property Total As Decimal
    Get
        For Each o In oc
            _total += o.Amount
        Next
        Return _total
    End Get
    Set(ByVal value As Decimal)
        _total = value
        OnPropertyChanged(New PropertyChangedEventArgs("Total"))
    End Set
End Property

Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
    If Not e Is Nothing Then
        RaiseEvent PropertyChanged(Me, e)
    End If
End Sub

End类

您可能要做的是将文本框和标签绑定到ListBox的SelectedItem属性

这里有一些XAML来告诉你我的意思

<StackPanel>
    <TextBlock Width="248" Height="24" Text="Colors:" TextWrapping="Wrap"/>
    <ListBox x:Name="lbColor" Width="248" Height="56">
        <ListBoxItem Content="Blue"/>
        <ListBoxItem Content="Green"/>
        <ListBoxItem Content="Yellow"/>
        <ListBoxItem Content="Red"/>
        <ListBoxItem Content="Purple"/>
        <ListBoxItem Content="Orange"/>
    </ListBox>
    <TextBlock Width="248" Height="24" Text="You selected color:" />
    <TextBlock Width="248" Height="24" Text={Binding ElementName="lbColor"}/>
</StackPanel>


要使标签成为值的总和,可以使用转换器并直接绑定到集合

第一部分是添加一个文本框,并将其绑定到ListBox上的SelectedItem属性。这将导致文本框显示所选项目的金额,并允许用户更新其值:

 <TextBox DataContext="{Binding ElementName=lb1, Path=SelectedItem}"
    Text="{Binding Path=Amount}" />

listboxitems是十进制值,我希望标签是listboxitems中值的总和。抱歉,遗漏了该部分。总数将是棘手的部分。因为双打没有改变比赛项目。
<TextBlock Text="{Binding Path=TotalAmount}"></TextBlock>
Public Class ViewModel Implements INotifyPropertyChanged
    ...
    Public Sub New()
        items = New ObservableCollection(Of TransactionModel)()
        Dim tm As New TransactionModel()
        tm.PropertyChanged += New PropertyChangedEventHandler(TransactionModel_PropertyChanged)
        items.Add(tm)
        tm = New TransactionModel()
        tm.PropertyChanged += New PropertyChangedEventHandler(TransactionModel_PropertyChanged)
        items.Add(tm)
        tm = New TransactionModel()
        tm.PropertyChanged += New PropertyChangedEventHandler(TransactionModel_PropertyChanged)
        items.Add(tm)
    End Sub

    Private Sub TransactionModel_PropertyChanged(sender As Object, e As PropertyChangedEventArgs)
        If e.PropertyName = "Amount" Then
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("TotalAmount"))
        End If
    End Sub
    ...
End Class