2011-01-26 144 views
0

在WPF窗口上,我有一个简单的十进制值列表框,它有一个绑定到它的一个ObservableCollection的Amounts,一个绑定到Total属性的标签,显示ListBox下面的值的总和,以及一个TextBox出现在绑定到selectedItem.Amount属性的ListBox的右侧。WPF与ObservableCollection的数据绑定

当我点击列表框中的一个项目时,我希望能够编辑被填充的文本框中的selectedItem的值,关闭文本框,并让listBoxItem更新其值,并且我希望总和得到更新在标签中也是如此。

我知道如何元素与元素绑定工作(即列表框到文本框) 什么我有麻烦搞清楚是元素与对象绑定(即ListBox中/的ObservableCollection的总资产)

谢谢非常!

这里有两个简单的类我到目前为止有:

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 

末级

回答

2

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

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

然后,您将需要一个名为总金额在视图模型的属性,并绑定到它的价值一个TextBlock。你还必须处理的PropertyChanged的“价值”,并重新引发该事件的“总金额”,这将导致视图刷新:

<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 
0

你可能想做的是b IND你的文本框和您的标签列表框

这里的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> 

,让您的标签值的总和,你可以使用转换器并直接绑定到集合。

+0

的listboxitems是十进制值,我希望标签是列表框中值的总和。 – 2011-01-26 23:37:19