绑定字典<string,list <class>&gt;到多个TreeViewItem

时间:2017-08-22 14:46:21

标签: c# wpf dictionary

所以我得到了一些我希望存储在树视图中的数据,并且我让它与2个分支机构合作,但我仍然坚持不止这样做。我认为字典最适合这个原因,当我加入“理智检查”时,它最容易将ID存储在密钥中

我希望如何呈现我的数据:

--Internet Checks
----- 'Date, message, status'
--------- 'Details'
--Server Checks
----- 'Date, message, status'
--------- 'Details'
--Sanity Checks
----- 'Date, message, status'
--------- 'Details'
--------- 'Error1'
--------- 'Error2'
--------- 'Error3'
--------- 'etc'
--etc

这就是目前的情况

--Internet Checks
----- 'Date, message, status'
--Server Checks
----- 'Date, message, status'
--Sanity Checks
----- 'Date, message, status'
--etc

所以现在我的xaml如何:

<TreeView Name="treeView1" ItemsSource="{Binding Items}">
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Path=Value.SubItems}">
                        <TextBlock Text="{Binding Value.Text}" />
                        <HierarchicalDataTemplate.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=Value.Text}"/>
                            </DataTemplate>
                        </HierarchicalDataTemplate.ItemTemplate>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>

这是我的MenuItem类:

public class MenuItem
    {
        public MenuItem()
        {
            this.SubItems = new Dictionary<int, MenuItem>();
        }

        public Dictionary<int, MenuItem> SubItems { get; set; }

        public string Text { get; set; }
    }

以下是我目前填写的字典:

public Dictionary<int, MenuItem> Fill(string name, Dictionary<int, MenuItem> root, List<HealthCheckHourModel> list)
        {
            root.Add(nextNum, new MenuItem { Text = name + " Checks"});
            foreach (HealthCheckHourModel hchm in list)
            {
                if (hchm.Message.Contains(name))
                {
                    root[nextNum].SubItems.Add(hchm.ID, new MenuItem { Text = hchm.StartTime + " " + hchm.Message });
                    root[nextNum].SubItems[hchm.ID].SubItems.Add(-111, new MenuItem { Text = hchm.Details });
                }
            }
            nextNum--;
            return root;
        }

1 个答案:

答案 0 :(得分:0)

有两种可能性:

  • 删除HierarchicalDataTemplate的ItemTemplate。

    <TreeView Name="treeView1" ItemsSource="{Binding Items}">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Path=Value.SubItems}">
                <TextBlock Text="{Binding Value.Text}" />
                <!--<HierarchicalDataTemplate.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Value.Text}"/>
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>-->
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>
    
  • 为HierarchicalDataTemplate保留ItemTemplate,但将DataTemplate替换为HierarchicalDataTemplate

    <TreeView Name="treeView1" ItemsSource="{Binding Items}">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Path=Value.SubItems}">
                <TextBlock Text="{Binding Value.Text}" />
                <HierarchicalDataTemplate.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Path=Value.SubItems}">
                        <TextBlock Text="{Binding Value.Text}" />
                    </HierarchicalDataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>