WPF Listview绑定到ItemSource?

时间:2010-02-08 12:51:07

标签: c# wpf listview binding

我有以下列表视图,但它不显示实际记录,而只显示对象的命名空间。我想知道是否需要在XAML中创建列以显示记录,然后将其绑定到对象的某些属性或者这有什么问题?

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

            ></ListView>

ListOfCustomersObservableCollection<Customer>类型。实际客户会加载到ObservableCollection中,但不会显示它们。缺少什么?

3 个答案:

答案 0 :(得分:39)

您还需要选择要显示的列:

<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>

答案 1 :(得分:4)

您也可以尝试

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

其中CustomerDataTemplate是Customer类的DataTemplate ...

答案 2 :(得分:0)

是不是因为您没有使用公开DataContext属性的实例(返回要显示的项目列表)设置ListView的ListOfCustomers属性?