2017-06-02 97 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