WPF分层数据模板ItemsControl [英] WPF HierarchicalDataTemplate & ItemsControl

查看:131
本文介绍了WPF分层数据模板ItemsControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含遵循此结构的对象的列表.这不是我正在使用的实际类,但是应该解释一下这个概念.

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.

public class BaseType{}
public class TypeA : BaseType{}
public class TypeB: BaseType
{
    public List<TypeA> TypeAList { get; private set; }
}

ItemsControl绑定到的列表是List<BaseType>

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>

现在,我希望看到的是在TypeB对象属性中找到的所有TypeA对象都将显示在ItemsControl中,而我只看到以为HierarchicalDataTemplate定义的样式显示的TypeB对象.我在TreeView控件中使用了相同的数据模板,在其中可以很好地显示子项.

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.

  • 您不能在ItemsControl中使用HierarchicalDataTemplate吗?
  • 如何在ItemsControl中显示父子关系?

推荐答案

您确实需要研究使用TreeView控件进行模板化和使用,或者构建自己的控件来使用分层数据.

You really need to look into templating and working with the TreeView control, or build your own control to work with the hierarchical data.

在某些情况下,您可以为常规项目控件设计自己的数据模板,该常规项目控件的嵌套控件绑定到该项目,例如(psuedo)

In some situations, you may be able to design your own data template for a regular items control whose nested controls bind to the item, such as (psuedo)

<HierarchicalDataTemplate>
    <Grid DataContext="{Binding}">
        <ListBox ItemsSource="{Binding TypeAList}" />
    </Grid>
</HierarchicalDataTemplate>

还没有尝试过上面的代码

该控件需要了解HierarchicalDataTemplate-在上面的示例中,该控件只是将其用作DataTemplate(HierarchicalDataTemplate是从DataTemplate派生的,以简化样式,并简化该数据模板的DependencyProperty的类型).

The control needs to be aware of the HierarchicalDataTemplate - and in the example above, the control is simply using it as a DataTemplate (HierarchicalDataTemplate derives from DataTemplate for simplicity in styles and the DependencyProperty's type for that data template).

这篇关于WPF分层数据模板ItemsControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆