WPF TreeView在HierarchicalDataTemplate中有多个列表

时间:2014-09-04 09:34:23

标签: c# wpf xaml treeview

我的模型是这样的:

public class CPFF
{
     public CMC Cmc { get; set; }
}

public class CMC 
{
     public List<CMCRecord> recordList { get; set; }
}

public class CMCRecord
{
     public string name { get; set; }
     public List<param> recordHeader { get; set; }
     public List<param> paramList { get; set; }
     public List<CmcStruct> structList { get; set; }
     public bool IsEmptyRecord { get; set; }
     public CpffConsts.CpffProjectVersion cpffProjectVersion { get; set; }
}

这是我的Xaml:

<TreeView  DataContext="{Binding Cpff1StProjectObject}" SelectedItemChanged="TreeView_OnSelectedItemChanged" HorizontalContentAlignment="Stretch" >
  <TreeViewItem Header="CMC" >
    <TreeViewItem Header="RecordList" ItemsSource="{Binding Cmc.recordList}" >
      <HierarchicalDataTemplate DataType="{x:Type types:CMCRecord}" ItemsSource="{Binding paramList}">
       <TextBlock Text="{Binding Path=name, StringFormat={}CMCRecord : {0}}" />
      </HierarchicalDataTemplate>
    </TreeViewItem>
  </TreeViewItem>
</TreeView>
<HierarchicalDataTemplate DataType="{x:Type types:param}">
  <StackPanel>
    <TextBlock Text="{Binding Path=name, StringFormat={}Name : {0}}" />
    <TextBlock Text="{Binding Path=type, StringFormat={}Type : {0}}" />
    <TextBlock Text="{Binding Path=description, StringFormat={}Description : {0}}" />
    <TextBlock Text="{Binding Path=defaultValue, StringFormat={}DefaultValue : {0}}" />
    <TextBlock Text="{Binding Path=domain_name, StringFormat={}domain_name : {0}}" />
    <TextBlock Text="{Binding Path=notes, StringFormat={}Notes : {0}}" />
    <TextBlock Text="{Binding Path=offset, StringFormat={}Offset : {0}}" />
    <TextBlock Text="{Binding Path=bytes, StringFormat={}Bytes : {0}}" />
    <TextBlock Text="{Binding Path=size, StringFormat={}Size : {0}}" />
    <TextBlock Text="{Binding Path=min, StringFormat={}Min : {0}}" />
    <TextBlock Text="{Binding Path=max, StringFormat={}Max : {0}}" />
  </StackPanel>
 </<HierarchicalDataTemplate DataType="{x:Type types:param}">
                <StackPanel>
                    <TextBlock Text="{Binding Path=name, StringFormat={}Name : {0}}" />
                    <TextBlock Text="{Binding Path=type, StringFormat={}Type : {0}}" />
                    <TextBlock Text="{Binding Path=description, StringFormat={}Description : {0}}" />
                    <TextBlock Text="{Binding Path=defaultValue, StringFormat={}DefaultValue : {0}}" />
                    <TextBlock Text="{Binding Path=domain_name, StringFormat={}domain_name : {0}}" />
                    <TextBlock Text="{Binding Path=notes, StringFormat={}Notes : {0}}" />
                    <TextBlock Text="{Binding Path=offset, StringFormat={}Offset : {0}}" />
                    <TextBlock Text="{Binding Path=bytes, StringFormat={}Bytes : {0}}" />
                    <TextBlock Text="{Binding Path=size, StringFormat={}Size : {0}}" />
                    <TextBlock Text="{Binding Path=min, StringFormat={}Min : {0}}" />
                    <TextBlock Text="{Binding Path=max, StringFormat={}Max : {0}}" />
                </StackPanel>
            </HierarchicalDataTemplate>>

这就是我树的样子: enter image description here

由于CMCRecord包含多个列表,我想将其反映到该工具中。 但是,由于HierarchicalDataTemplate中的ItemSource绑定到参数列表, 如何在TreeView中显示多个列表?

1 个答案:

答案 0 :(得分:1)

您需要创建各种列表的联合作为类型对象的列表(即List<object>)并将其设置为Children中的HierarchicalDataTemplate目标并使用WPF类型推断以确定要使用的HierarchicalDataTemplate,就像您对类型param所做的那样