2010-02-08 118 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 ...