Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HierarchycalDataTemplate不工作_C#_Wpf_Xaml_Hierarchicaldatatemplate - Fatal编程技术网

C# HierarchycalDataTemplate不工作

C# HierarchycalDataTemplate不工作,c#,wpf,xaml,hierarchicaldatatemplate,C#,Wpf,Xaml,Hierarchicaldatatemplate,我试图使用HierarchycalDataTemplate递归地创建包含项目的展开器,但是当我使用HierarchycalDataTemplate时,我只显示第一级项目 如果你需要更多的信息,请告诉我 下面是如果我手写xaml的样子: <GroupBox Header="SectionHeader"> <StackPanel > <Expander VerticalAlignment="Top" Header="SubSectionHeade

我试图使用HierarchycalDataTemplate递归地创建包含项目的展开器,但是当我使用HierarchycalDataTemplate时,我只显示第一级项目

如果你需要更多的信息,请告诉我

下面是如果我手写xaml的样子:

<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>
这是我到目前为止得到的

Xaml:


数据类:

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;
        }
    }
}
类TopViewModel
{
ObservableCollection_sections=新的ObservableCollection();
公共收集组
{
得到
{
返回部分;
}
设置
{
_截面=值;
}
}
}
公共类分区视图模型
{
ObservableCollection_materials=新的ObservableCollection();
ObservableCollection_=新的ObservableCollection();
第(u)节;;
公共部门
{
得到
{
返回部分;
}
设置
{
_截面=值;
}
}
公共字符串材质名称
{
获取{return Section.SectionName;}
设置{Section.SectionName=value;}
}
公开收集资料
{
得到
{
退料;
}
设置
{
_材料=价值;
}
}
公开收集
{
得到
{
返回(u);;
}
设置
{
_分段=值;
}
}
}
公共类子集视图模型
{
ObservableCollection_materials=新的ObservableCollection();
ObservableCollection_=新的ObservableCollection();
第(u)款;;
公开收集资料
{
得到
{
退料;
}
设置
{
_材料=价值;
}
}
公开收集
{
得到
{
返回(u);;
}
设置
{
_分段=值;
}
}
公共分部
{
得到
{
返回(u)小节;;
}
设置
{
_分段=价值;
}
}
}

截面。截面名称应为材料名称

视线:

    <ItemsControl ItemsSource="{Binding SubSections}" ItemTemplate="{StaticResource BinderTemplate}" />
你也许应该写信

    {Binding}
我在您的类中找不到SubSectionName


请添加更多类

节。节名应为MaterialName

视线:

    <ItemsControl ItemsSource="{Binding SubSections}" ItemTemplate="{StaticResource BinderTemplate}" />
你也许应该写信

    {Binding}
我在您的类中找不到SubSectionName


请添加更多类

除了
HierarchycalDataTemplate
之外,另一种方法是将
项控件
添加到数据模板中的
扩展器
,并将
项源
绑定到该数据模板中

此外,由于您在数据模板定义中指定了
数据类型
,因此您不必直接按顶级
项目控件的键名设置
项目模板

如果您这样修改XAML,它可能会执行您想要的操作:

<DataTemplate DataType="{x:Type local:SubSectionViewModel}">
    <StackPanel>
        <Expander Header="{Binding SubSection.SubSectionName}">
            <ItemsControl ItemsSource="{Binding SubSections}"/>
        </Expander>
    </StackPanel>
</DataTemplate>

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

HierarchycalDataTemplate
的另一种替代方法是将
项控件
添加到数据模板中的
扩展器
,并将
项源
绑定到该数据模板中

此外,由于您在数据模板定义中指定了
数据类型
,因此您不必直接按顶级
项目控件的键名设置
项目模板

如果您这样修改XAML,它可能会执行您想要的操作:

<DataTemplate DataType="{x:Type local:SubSectionViewModel}">
    <StackPanel>
        <Expander Header="{Binding SubSection.SubSectionName}">
            <ItemsControl ItemsSource="{Binding SubSections}"/>
        </Expander>
    </StackPanel>
</DataTemplate>

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

您的
层次结构数据模板中缺少一个关键位
-如何呈现子元素:

<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>

您的
HierarchycalDataTemplate
中缺少一个关键位-如何呈现子元素:

<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>

如果您希望在参考资料部分将所有数据模板放在一起,那么您还需要第一个数据模板上的数据类型,以便模板->类型推断能够工作(请参见我的编辑)。但是在我发布的代码中,由于
SectionViewModel
的模板是直接在ItemsControl中指定的,而不是在参考资料中指定的,所以它应该仍然有效,对吗?至少在我的测试中是这样的。有趣..当我把一个快速的骨架混合在一起时,它没有推断出模板类型…奇怪。您使用的是哪个版本?我是用VS2010测试的,没有使用Blend.Blend 4-虽然我想我们完全有可能在xaml中略有不同-我没有保存我的小工具,所以我在回答中发布的是剩下的所有内容:(您还需要第一个DataTemplate上的数据类型,才能使用template->type推断(请参见我的编辑)同意,如果您希望所有数据模板都在参考资料部分中。但是在我发布的代码中,由于
SectionViewModel
的模板直接在ItemsControl中指定,而不是在参考资料中指定,它应该仍然可以工作,对吗?它看起来至少在我的测试中可以工作。哈。有趣。它没有推断出模板当我在Blend中将一个快速骨架组合在一起时键入…奇怪。你在使用哪个版本?我在用VS2010进行测试,而不是使用Blend。Blend 4-虽然我认为我们完全可能在xaml中略有不同-我没有保存我的小工具,所以我在我的答案中发布的是剩下的所有内容。:(太棒了。谢谢:)我尝试过这样做,但尝试将模板用作静态res