SireChicken
SireChicken

Reputation: 55

Bound String List displays containing class name not the string

My Problem is the following:

I have a class which has a ObservableCollection of strings, which gets populated by the selected children.

Those are supposed to be listed in a ListBox.

But the strings aren't listed, rather the object type in FilterList is shown.

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

Filter List contains the classes which have ListOfActiveFilters as a Property. I thought that it would work the way I showed it, but it doesn't. Since that is also the way I saw others do it.

If I were to use a Class with a single string Property as the type of the Collection instead of the Collection of strings I have right now, I think it would work.

I just don't really see the point in creating a class which holds a string property just so that I can bind the ContentPresenter or TextBox to that property.

What am I doing wrong? I am rather new to the topic.

Upvotes: 1

Views: 113

Answers (1)

mm8
mm8

Reputation: 169350

One single ListBox will only be able to display the filters in one single ListOfActiveFilters collection. You could use nested ItemsControls to display them all:

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

The other option is to modify your data model and put all strings in a single collection. You could then bind the ItemsSource property of the ListBox directly to this one. But a ListBox itself has no concept of hierarhical data.

Upvotes: 1

Related Questions