2017-03-14 98 views
0

這是自定義對象:如何從組合框綁定對象數據到列表框

public class FileItem 
{ 
    public string Name { get; set; } 
    public string Path { get; set; } 
} 

Collection

public ObservableCollection<FileItem> collection { get; set; } 

Combobox

<ComboBox 
    Name="cbCollection" 
    ItemsSource="{Binding interfaces}"/> 

ListBox

<ListBox 
     Name="lbCollection " 
     ItemsSource="{Binding collection}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Name}"/> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

所以我的Combobox填充了我的object集合,我想查看我的ListBox中的所有屬性。

目前:

  1. 我只能看到屬性Name
  2. 我可以看到我的Combobox中的所有對象,而不僅僅是選定的對象。

回答

0

如果您不僅想看到name屬性,還需要擴展數據模板以包含其他屬性。

如果要查看選定項目的屬性,則應綁定到組合框的SelectedItem屬性。其實我不認爲你想要一個ListBox,因爲只有一個選定的項目。

這應該讓你開始:

<ContentControl Content="{Binding ElementName=cbCollection, Path=SelectedItem}"> 
    <ContentControl.Resources> 
    <DataTemplate DataType="{x:Type local:FileItem}"> 
     <TextBlock Text="{Binding Name}"/> 
    </DataTemplate> 
    </ContentControl.Resources> 
</ContentControl> 
+0

,我需要把這個ContentControl中? –

+0

用你的ListBox替換它。 –

+0

錯誤值不能爲空。 參數名稱:dataType和錯誤添加到IDictionary中的所有對象都必須具有Key屬性或與其關聯的其他類型的鍵。 –