如何在XAML中绑定多个ObservableCollection

时间:2012-02-04 06:11:15

标签: wpf vb.net observablecollection

如何在我的xaml中实现这两个ObservableCollections?我希望我的所有学生都有一个列表视图,还有我老师的另一个列表视图。现在我只是做了一个示例学生和教师对象。我是以错误的方式来做这件事的。最终我将从数据表中提取,但不知道如何将它实现到ObservableCollection。

     Public Class PersonalViewModel


Public Sub New()

    Dim obcollection1 As New ObservableCollection(Of Student)
    Dim obcollection2 As New ObservableCollection(Of Teacher)

    obcollection1.Add(New Student("W0332309", "Tony", "Thetiger", "Male"))

    obcollection2.Add(New Teacher)

    'I Cant set datacontext here cause 1.) this isn't a window 2.) I can only set datacontext once anyway
    'So how to do it in xaml?


End Sub

End Class

XAML:

    <Page.Resources>
    <local:PersonalViewModel x:Key="personalviewmodel"></local:PersonalViewModel>

</Page.Resources>

    <ListView ItemsSource="{Binding Source= {StaticResource personalviewmodel}}"   HorizontalAlignment="Left" Margin="256,42,0,67" Name="ListView1" Width="166" IsSynchronizedWithCurrentItem="True" >
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FName}" />
                    <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LName}" />
                </GridView>

            </ListView.View>
         </ListView>

1 个答案:

答案 0 :(得分:4)

您应该将两个集合设为ViewModel的公共成员。然后在XAML中将DataContext设置为ViewModel,并将Listbox绑定到绑定模型中的相关公共成员。

更新:这是一个简单但希望完整的示例

这是模型代码,而不是我只定义了一个Student类,并演示了一个非常天真的INotifyPropertyChanged实现。我重复使用Student课程进行教师收藏,所以我希望这不会引起混淆。

Imports System.ComponentModel
Imports System.Collections.ObjectModel

Public Class PersonalViewModel
  Private _students As New ObservableCollection(Of Student)
  Public ReadOnly Property Students As ObservableCollection(Of Student)
    Get
      Return _students
    End Get
  End Property

  Private _teachers As New ObservableCollection(Of Student)
  Public ReadOnly Property Teachers As ObservableCollection(Of Student) ' This should be a Teacher class
    Get
      Return _teachers
    End Get
  End Property

  Public Sub New()
    _students.Add(New Student("Tony", "Thetiger"))
    _teachers.Add(New Student("Cindy", "Thecougar"))
  End Sub
End Class

Public Class Student
  Implements INotifyPropertyChanged

  Private _firstName As String
  Private _lastName As String

  Public Sub New(firstName As String, lastName As String)
    Me.FirstName = firstName
    Me.LastName = lastName
  End Sub

  Public Property FirstName As String
    Get
      Return _firstName
    End Get
    Set(value As String)
      If value <> _firstName Then
        _firstName = value
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("FirstName"))
      End If
    End Set
  End Property

  Public Property LastName As String
    Get
      Return _lastName
    End Get
    Set(value As String)
      If value <> _lastName Then
        _lastName = value
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("LastName"))
      End If
    End Set
  End Property

  Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class

对于XAML,我刚刚使用了一个Window,但同样适用于Page。请注意,我将网格容器绑定到Model,然后将每个ListView绑定到相应的集合。

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ModelBindDemo"
    Title="MainWindow" Height="350" Width="525">
  <Window.Resources>
    <local:PersonalViewModel x:Key="model" />
  </Window.Resources>
  <Grid DataContext="{Binding Source={StaticResource model}}">
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*" />
      <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <ListView Grid.Column="0" ItemsSource="{Binding Students}">
      <ListView.View>
        <GridView>
          <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" />
          <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" />
        </GridView>
      </ListView.View>      
    </ListView>

    <ListView Grid.Column="1" ItemsSource="{Binding Teachers}">
      <ListView.View>
        <GridView>
          <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" />
          <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" />
        </GridView>
      </ListView.View>
    </ListView>
  </Grid>
</Window>