2010-02-08 98 views
14

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

<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中,但它们不会显示。什么不见​​了?

回答

35

您需要选择要显示的列,以及:

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

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

+0

我已经设置窗口的datacontext到包含该属性的类,应该不够吗? – 2010-02-08 12:58:50

+0

@Tony - 是的应该。它应该冒泡找到数据上下文。似乎你已经从acc回答中解决了问题。有什么问题? – Gishu 2010-02-08 13:15:12

+0

问题在于我的列表视图中没有创建与我的Customer类绑定的列。 – 2010-02-08 14:39:08

4

您也可以尝试

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

其中CustomerDataTemplate是Customer类一个DataTemplate ...