【问题标题】:How to bind more than one ObservableCollection in XAML如何在 XAML 中绑定多个 ObservableCollection
【发布时间】:2012-02-04 06:11:15
【问题描述】:

如何在我的 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>

【问题讨论】:

    标签: wpf vb.net observablecollection


    【解决方案1】:

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

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

    这里是Model代码,并不是我只定义了一个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,我刚刚使用了一个窗口,但同样适用于页面。请注意,我将网格容器绑定到模型,然后将每个 ListViews 绑定到各自的集合。

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

    【讨论】:

    • 你的意思是像创建两个单独的集合属性?你可以给我一个例子吗? :)
    • @TMan,正确。我整理一个例子贴在这里,我一般使用C#,所以我会尽力在VB.NET翻译。
    • 完美..非常感谢,就是这样。我在代码中是正确的,我只是不知道如何在 xaml 中实现它。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多