views:

807

answers:

3

Hi,

I have the following listview, but it doesn't show the actual records, but only the namespace of the object. I wondered if I need to create the columns in XAML for it to show the records and then bind it to some properties of an object or what is wrong with this?

<ListView
            Name="ListCustomers"
            ItemsSource="{Binding Path=ListOfCustomers}"
            SelectedItem="{Binding Path=SelectedCustomer}"
            SelectionMode="Single"
            IsSynchronizedWithCurrentItem="True"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            MinHeight="100"

            ></ListView>

ListOfCustomers is an ObservableCollection<Customer> type. The actual customers do get loaded into the ObservableCollection, but they are not displayed. What is missing?

A: 

Is it because you have not set the DataContext property of the ListView with the instance that exposes the ListOfCustomers property (which returns the list of items to be displayed) ?

Gishu
Yes ListOfCustomers returns items to be displayed.
Tony
I have set the datacontext of the window to the class that contains the property, shouldn't that be enough?
Tony
@Tony - yes should be. It should bubble up to find the data context. Seems that you have it solved from the acc answer. What was the problem?
Gishu
Problem was that I had no columns created in my listview that are bound to my Customer class.
Tony
+3  A: 

This tutorial walks through a method, but it does select the columns to display as well:

<ListView ItemsSource="{Binding GameCollection}">
  <ListView.View>
    <GridView>
      <GridViewColumn Width="140" Header="Game Name"
         DisplayMemberBinding="{Binding GameName}"  />
      <GridViewColumn Width="140" Header="Creator"  
         DisplayMemberBinding="{Binding Creator}" />
      <GridViewColumn Width="140" Header="Publisher"
         DisplayMemberBinding="{Binding Publisher}" />
    </GridView>
  </ListView.View>
</ListView>

This page gives a slightly different solution.

ChrisF
+3  A: 

You could also try

<ListView
.
.
ItemTemplate="{StaticResource CustomerDataTemplate}"
.
.
/>

where CustomerDataTemplate is a DataTemplate for Customer class...

miensol