I have a list containing objects that follow this structure. This is not the real classes I am working with, but should explain the concept.
CLASSES
public class BaseType{}
public class TypeA : BaseType{}
public class TypeB: BaseType
{
public List<TypeA> TypeAList { get; private set; }
}
The List that the ItemsControl binds to is a List<BaseType>
XAML
<ItemsControl>
<ItemsControl.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:TypeB}" ItemsSource = "{Binding Path=TypeAList}">
<DataTemplate.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="18"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</DataTemplate.Resources>
<Grid>
<Ellipse Fill="Gold"/>
<StackPanel>
<TextBlock Margin="3,3,3,0"
Text="{Binding Path=Description}"/>
<TextBlock Margin="3,0,3,7"
Text="{Binding Path=Name}"/>
</StackPanel>
</Grid>
</HierarchicalDataTemplate>
<ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Now what I would expect to see is all the TypeA objects found in the TypeB object property to be displayed in the ItemsControl, instead I only see the TypeB objects, displayed with the styles defined for the HierarchicalDataTemplate. i've used the same datatemplate in a TreeView control, where it displays the child items fine.
- Can you not use a HierarchicalDataTemplate in an ItemsControl?
- How do you go about displaying a parent child relationship in an ItemsControl?