如何在 XAML 中绑定多个 ObservableCollection

TMa*_*Man 2 vb.net wpf 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
Run Code Online (Sandbox Code Playgroud)

xml:

    <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>
Run Code Online (Sandbox Code Playgroud)

Chr*_*lor 5

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

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

这是模型代码,不是我只定义了一个 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
Run Code Online (Sandbox Code Playgroud)

对于 XAML,我刚刚使用了一个 Window,但同样适用于 Page。请注意,我将网格容器绑定到模型,然后将每个 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>
Run Code Online (Sandbox Code Playgroud)