views:

21

answers:

1

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>
+2  A: 

A couple of things:

  1. Set the ListView.ItemsSource equal to the collection of Student objects.
  2. 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>
karmicpuppet