如何将我的自定义类(Ex:Student)对象的列表绑定到WPF中的列表视图

时间:2010-08-25 20:27:03

标签: wpf data-binding listview

如何将自定义类(Ex:Student)对象的列表绑定到WPF中的列表视图? 我的XAML标记:ListView在这里。我想在Listview中显示用户,如html表

ListView Height="100" HorizontalAlignment="Left" Margin="27,98,0,0" Name="listView1" VerticalAlignment="Top" Width="320">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="160" Header="Name"></GridViewColumn>
                <GridViewColumn Width="160" Header="Age"></GridViewColumn>

            </GridView>
        </ListView.View>
    </ListView>

1 个答案:

答案 0 :(得分:2)

有几件事:

  1. 将ListView.ItemsSource设置为等于Student对象的集合。
  2. 设置GridViewColumn的DisplayMemberBinding属性。 (另一种方法是设置CellTemplate属性。)
  3. 将这两个应用于您的样本XAML:

    <ListView Height="100" HorizontalAlignment="Left" Margin="27,98,0,0" Name="listView1" VerticalAlignment="Top" Width="320" ItemsSource="{Binding StudentCollection}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Width="160" Header="Name" DisplayMemberBinding="{Binding Name}"></GridViewColumn>
                        <GridViewColumn Width="160" Header="Age" DisplayMemeberBinding="{Binding Age}"></GridViewColumn>
    
                    </GridView>
                </ListView.View>
            </ListView>