So what if you want multiple items within the
Header/Content? You could use an ItemsControl, like the HeaderedItemsControl
class. This control already provides this ability with ease. However, using
the HeaderedContentControl, the solution can also be to use an ItemsControl
within the content. To setup this example, suppose you had the following:
Listing 5: Example Class
public class SampleClass
{
public string Title { get; set; }
public string Text { get; set; }
public List<SampleItem> Items { get; set; }
}
Public class SampleItem
{
public string Name { get; set; }
public string Value { get; set; }
}
Using a similar setup previously for the header/content, the
solution to binding a repetitive list within the HeaderedContentControl control
is to use a repetitive control (like the ListBox control) within the content.
<HeaderedContentControl Header="{Binding Title}">
<HeaderedContentControl.Content>
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding Name}" />
<Label Content="{Binding Value}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</HeaderedContentControl.Content>
</HeaderedContentControl>
In this example, the ListBox control is a way to show
multiple items within the content. The source of the content comes from the
Items property in the SampleClass class. Any items in this list is bound to
the ListBox, which each item gets its own instance of the provided
DataTemplate, binding each SampleItem object to the created template for the
ListBoxItem.