How can i bind a list of my custom class(Ex :Student) objects to a list view in WPF
How can i bind a list of my custom class(Ex :Student) objects to a list view in WPF ? My XAML markup for :ListView is here.I want to show the users in the Listview like a html table
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>
A couple of things:
- Set the ListView.ItemsSource equal to the collection of Student objects.
- Set the DisplayMemberBinding property of the GridViewColumn. (An alternative is to set the CellTemplate property).
Applying these two to your sample 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>
精彩评论