DataTemplate for Listbox containing lists as items

2019-08-21 18:42发布

问题:

I have the following data template for a list box items:

<DataTemplate x:Key="substanceListShower">
    <ListBox ItemsSource="{Binding Items}">
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox>
</DataTemplate>

And then I apply the item template like this:

 ReactantInterfacesListBox.ItemTemplate = (DataTemplate)FindResource("substanceListShower");

But in the list for the items i get a ToString() return:

System.Windows.Controls.ItemsPanelTemplate

Any help would be appreciated.

回答1:

You add an ItemsPanelTemplate to your ListBox, your definition lacks the ListBox.ItemsPanel tags to set the property instead.