我有一个列表,其中包含遵循这种结构的对象。这不是我正在工作的真正的课程,但是应该解释这个概念。
类
public class BaseType{}
public class TypeA : BaseType{}
public class TypeB: BaseType
{
public List<TypeA> TypeAList { get; private set; }
}ItemsControl绑定到的列表是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>现在,我希望看到的是,在TypeA对象属性中找到的所有TypeB对象都将显示在ItemsControl中,而只看到TypeB对象,这些对象具有为HierarchicalDataTemplate定义的样式。我在TreeView控件中使用了相同的数据板,它可以很好地显示子项。
您可以在ItemsControl?
中显示父级子关系吗?
发布于 2009-08-06 04:11:12
您确实需要研究模板化和使用TreeView控件,或者构建自己的控件来处理分层数据。
在某些情况下,您可以为其嵌套控件绑定到该项的常规项控件(如(psuedo) )设计自己的数据模板。
<HierarchicalDataTemplate>
<Grid DataContext="{Binding}">
<ListBox ItemsSource="{Binding TypeAList}" />
</Grid>
</HierarchicalDataTemplate>还没有试过上面的代码
控件需要知道HierarchicalDataTemplate --在上面的示例中,控件只是将其用作一个DataTemplate (HierarchicalDataTemplate源自于DataTemplate,用于简化样式,而DependencyProperty的类型用于该数据模板)。
https://stackoverflow.com/questions/1222008
复制相似问题