2017-06-02 64 views
1

我的問題是這樣的:約束字符串列表不顯示字符串

我有有字符串的ObservableCollection,它獲取由選定的子居住的類。

這些應該列在列表框中。

但是沒有列出字符串,而是顯示了FilterList中的對象類型。

<ListBox Grid.Row="2" Grid.Column="0" 
      ItemsSource="{Binding FilterList}"> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel IsItemsHost="True" 
          Orientation="Horizontal"/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
     <ListBox.ItemTemplate> 
      <HierarchicalDataTemplate ItemsSource="{Binding ListOfActiveFilters}"> 
       <!--<ContentPresenter Content="{Binding ListOfActiveFilters.}"/>--> 
        <TextBlock Text="{Binding}" /> 
      </HierarchicalDataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

過濾器列表包含具有ListOfActiveFilters作爲屬性的類。 我認爲它會按照我展示的方式工作,但事實並非如此。因爲這也是我看到別人這樣做的方式。

如果我要使用一個具有單個字符串Property的類作爲Collection的類型而不是字符串的集合,我現在已經有了,我認爲它會起作用。

我只是沒有真正看到創建一個持有字符串屬性的類,以便我可以將ContentPresenter或TextBox綁定到該屬性。

我在做什麼錯?我對這個話題頗爲陌生。

回答

1

一個單一的ListBox將只能夠在一個單一的ListOfActiveFilters集合中顯示過濾器。你可以使用嵌套ItemsControls來顯示它們:

<ItemsControl ItemsSource="{Binding FilterList}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <ListBox ItemsSource="{Binding ListOfActiveFilters}"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding}" /> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

另一種選擇是修改您的數據模型,並把所有的字符串在一個單一的集合。然後,您可以將ListBoxItemsSource屬性直接綁定到該屬性。但是一個ListBox本身沒有分層數據的概念。

+0

好吧,我不知道。 謝謝 – SireChicken