HierarchicalDataTemplate不工作 [英] HierarchicalDataTemplate not working

查看:156
本文介绍了HierarchicalDataTemplate不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个HierarchicalDataTemplate递归创建在他们的项目扩展,但是当我用HierarchicalDataTemplate我只得到显示项目的第一级。



请让我知道如果你需要anyore信息。



继承人的什么XAML会是什么样子,如果我分组框标题=SectionHeader>

 <的:手写
<&StackPanel的GT;
<扩展VerticalAlignment =评出的标题=SubSectionHeader>
<&StackPanel的GT;
<扩展VerticalAlignment =评出的标题=SubSectionHeader保证金=10,0,0,0>
<&StackPanel的GT;
等......
< / StackPanel的>
< /扩展>
< /扩展>
< / StackPanel的>
< /分组框中>



下面有什么,我有这么远。



XAML中:

 < ItemsControl的名称=lstMain的ItemsSource ={结合部分}> 
< ItemsControl.ItemTemplate>
<&DataTemplate的GT;
<分组框标题={结合Section.SectionName}>
<的ItemsControl的ItemsSource ={结合小节}的ItemTemplate ={StaticResource的BinderTemplate}/>
< /分组框中>
< / DataTemplate中>
< /ItemsControl.ItemTemplate>
< / ItemsControl的>

< HierarchicalDataTemplate X:键=BinderTemplate的ItemsSource ={绑定路径=小节}数据类型={X:类型本地:SubSectionViewModel}>
<&StackPanel的GT;
<扩展报头={结合SubSection.SubSectionName}/>
< / StackPanel的>
< / HierarchicalDataTemplate>



数据类:

 类TopViewModel 
{
&的ObservableCollection LT; SectionViewModel> _sections =新的ObservableCollection< SectionViewModel>();

公众的ObservableCollection< SectionViewModel>第
{
得到
{
返回_sections;
}

{
_sections =价值;
}
}
}

公共类SectionViewModel
{
&的ObservableCollection LT; MaterialViewModel> _materials =新的ObservableCollection< MaterialViewModel>();
&的ObservableCollection LT; SubSectionViewModel> _subSections =新的ObservableCollection< SubSectionViewModel>();
节_section;

公共部门第
{
得到
{
返回_section;
}

{
_section =价值;
}
}

公共字符串MaterialName
{
{返回Section.SectionName; }
集合{Section.SectionName =价值; }
}

公众的ObservableCollection< MaterialViewModel>材料
{
得到
{
返回_materials;
}

{
_materials =价值;
}
}

公众的ObservableCollection< SubSectionViewModel>小节
{
得到
{
返回_subSections;
}

{
_subSections =价值;
}
}
}

公共类SubSectionViewModel
{
&的ObservableCollection LT; MaterialViewModel> _materials =新的ObservableCollection< MaterialViewModel>();
&的ObservableCollection LT; SubSectionViewModel> _subSections =新的ObservableCollection< SubSectionViewModel>();
款_subSection;

公众的ObservableCollection< MaterialViewModel>材料
{
得到
{
返回_materials;
}

{
_materials =价值;
}
}

公众的ObservableCollection< SubSectionViewModel>小节
{
得到
{
返回_subSections;
}

{
_subSections =价值;
}
}

款大众款
{
得到
{
返回_subSection;
}

{
_subSection =价值;
}
}
}


解决方案

您缺少在 HierarchicalDataTemplate A键位 - 如何渲染子元素:

 < HierarchicalDataTemplate X:键=BinderTemplate
的ItemsSource ={绑定路径=小节}
数据类型={X:类型本地:SubSectionViewModel}>
<&StackPanel的GT;
<扩展报头={结合SubSection.SubSectionName}>
< ItemsControl的保证金=5,0,0,0
的ItemsSource ={结合小节}
的ItemTemplate ={DynamicResource BinderTemplate}/>
< /扩展>
< / StackPanel的>
< / HierarchicalDataTemplate>



编辑:不偷@ BDE的风头,但他/她对使用的大多是正确的数据类型 - 但这是你简化的方式,上述XAML:

 <窗口x:类=WpfApplication1.Window1
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =HTTP: //schemas.microsoft.com/winfx/2006/xaml
的xmlns:地方=CLR的命名空间:WpfApplication1
标题=窗口1HEIGHT =300WIDTH =300>
< Window.Resources>
<! - 对部分普通模板 - >
<数据类型的DataTemplate ={X:类型本地:SectionViewModel}>
<分组框标题={结合Section.SectionName}>
<的ItemsControl的ItemsSource ={结合小节}/>
< /分组框中>
< / DataTemplate中>
<! - 为小节分层模板 - >
< HierarchicalDataTemplate
数据类型={X:类型本地:SubSectionViewModel}>
<&StackPanel的GT;
<扩展报头={结合SubSection.SubSectionName}>
< ItemsControl的保证金=5,0,0,0
的ItemsSource ={结合小节}/>
< /扩展>
< / StackPanel的>
< / HierarchicalDataTemplate>
< /Window.Resources>
<网格和GT;
< - !
无需指定的ItemTemplate如果能WPF苏斯所有的项目类型
- >
< ItemsControl的名称=lstMain的ItemsSource ={结合}节/>
< /网格和GT;
< /窗GT;


I am trying to use a HierarchicalDataTemplate to recursively create expanders with items in them but when I use the HierarchicalDataTemplate I only get the first level of items displayed.

Please let me know if you need anyore information.

Heres the what the xaml would look like if I was writing by hand:

<GroupBox Header="SectionHeader">
    <StackPanel >
        <Expander VerticalAlignment="Top" Header="SubSectionHeader">
            <StackPanel>
                <Expander VerticalAlignment="Top" Header="SubSectionHeader" Margin="10,0,0,0">
                    <StackPanel>
                        etc......
                    </StackPanel>
                </Expander>
         </Expander>
    </StackPanel>
</GroupBox>

Heres what I have so far.

Xaml:

<ItemsControl Name="lstMain" ItemsSource="{Binding Sections}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <GroupBox Header="{Binding Section.SectionName}">
                <ItemsControl ItemsSource="{Binding SubSections}" ItemTemplate="{StaticResource BinderTemplate}" />
            </GroupBox>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

<HierarchicalDataTemplate x:Key="BinderTemplate" ItemsSource="{Binding Path=SubSections}" DataType="{x:Type local:SubSectionViewModel}">
    <StackPanel>
        <Expander Header="{Binding SubSection.SubSectionName}"/>
    </StackPanel>
</HierarchicalDataTemplate>

Data Classes:

class TopViewModel
{
    ObservableCollection<SectionViewModel> _sections = new ObservableCollection<SectionViewModel>();

    public ObservableCollection<SectionViewModel> Sections
    {
        get
        {
            return _sections;
        }
        set
        {
            _sections = value;
        }
    }
}

public class SectionViewModel
{
    ObservableCollection<MaterialViewModel> _materials = new ObservableCollection<MaterialViewModel>();
    ObservableCollection<SubSectionViewModel> _subSections = new ObservableCollection<SubSectionViewModel>();
    Section _section;

    public Section Section
    {
        get
        {
            return _section;
        }
        set
        {
            _section = value;
        }
    }

    public string MaterialName
    {
        get { return Section.SectionName; }
        set { Section.SectionName = value; }
    }

    public ObservableCollection<MaterialViewModel> Materials
    {
        get
        {
            return _materials;
        }
        set
        {
            _materials = value;
        }
    }

    public ObservableCollection<SubSectionViewModel> SubSections
    {
        get
        {
            return _subSections;
        }
        set
        {
            _subSections = value;
        }
    }
}

public class SubSectionViewModel
{
    ObservableCollection<MaterialViewModel> _materials = new ObservableCollection<MaterialViewModel>();
    ObservableCollection<SubSectionViewModel> _subSections = new ObservableCollection<SubSectionViewModel>();
    SubSection _subSection;

    public ObservableCollection<MaterialViewModel> Materials
    {
        get
        {
            return _materials;
        }
        set
        {
            _materials = value;
        }
    }

    public ObservableCollection<SubSectionViewModel> SubSections
    {
        get
        {
            return _subSections;
        }
        set
        {
            _subSections = value;
        }
    }

    public SubSection SubSection
    {
        get
        {
            return _subSection;
        }
        set
        {
            _subSection = value;
        }
    }
}

解决方案

You are missing a key bit in your HierarchicalDataTemplate - how to render the subelements:

<HierarchicalDataTemplate x:Key="BinderTemplate" 
    ItemsSource="{Binding Path=SubSections}" 
    DataType="{x:Type local:SubSectionViewModel}">
    <StackPanel>
        <Expander Header="{Binding SubSection.SubSectionName}">
            <ItemsControl Margin="5,0,0,0" 
                      ItemsSource="{Binding SubSections}" 
                      ItemTemplate="{DynamicResource BinderTemplate}"/>
        </Expander>
    </StackPanel>
</HierarchicalDataTemplate>

EDIT: Not to steal @BDE's thunder, but he/she's mostly correct about the use of DataType - but this is the way you'd "simplify" the above XAML:

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:WpfApplication1" 
        Title="Window1" Height="300" Width="300">
    <Window.Resources>
         <!-- normal template for sections -->
         <DataTemplate DataType="{x:Type local:SectionViewModel}">
            <GroupBox Header="{Binding Section.SectionName}">
                <ItemsControl ItemsSource="{Binding SubSections}"/>
            </GroupBox>
         </DataTemplate>
         <!-- hierarchical template for subsections -->
         <HierarchicalDataTemplate 
              DataType="{x:Type local:SubSectionViewModel}">
           <StackPanel>
               <Expander Header="{Binding SubSection.SubSectionName}">
                   <ItemsControl Margin="5,0,0,0" 
                         ItemsSource="{Binding SubSections}"/>
               </Expander>
           </StackPanel>
        </HierarchicalDataTemplate>
    </Window.Resources>
    <Grid>
        <!-- 
           no need to specify ItemTemplate if WPF can suss out all the item types 
         -->
        <ItemsControl Name="lstMain" ItemsSource="{Binding Sections}"/>
    </Grid>
</Window>

这篇关于HierarchicalDataTemplate不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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