WPF Listview binding to ItemSource?

98,410

Solution 1

You need to select the columns to display as well:

<ListView ItemsSource="{Binding ListOfCustomers}"
          SelectedItem="{Binding Path=SelectedCustomer}"
          ....>
  <ListView.View>
    <GridView>
      <GridViewColumn Width="140" Header="First Name"
         DisplayMemberBinding="{Binding FirstName}"  />
      <GridViewColumn Width="140" Header="Last Name"  
         DisplayMemberBinding="{Binding LastName}" />
      <GridViewColumn Width="140" Header="Email Address"
         DisplayMemberBinding="{Binding Email}" />
      ....
    </GridView>
  </ListView.View>
</ListView>

Solution 2

You could also try

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

where CustomerDataTemplate is a DataTemplate for Customer class...

Share:
98,410
Tony The Lion
Author by

Tony The Lion

#disgusted

Updated on April 27, 2020

Comments

  • Tony The Lion
    Tony The Lion about 4 years

    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?

  • Tony The Lion
    Tony The Lion over 14 years
    I have set the datacontext of the window to the class that contains the property, shouldn't that be enough?
  • Gishu
    Gishu over 14 years
    @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?
  • Tony The Lion
    Tony The Lion over 14 years
    Problem was that I had no columns created in my listview that are bound to my Customer class.