C# WPF - 使用字典绑定TreeView

3
我需要用我拥有的字典列表填充一个TreeView。
List<Dictionary<string,object>> 

在字典中,有标题和子键

[{"title":"foo", "children":[]},]

然而,我无法确定绑定关系。这是完全错误的。我需要明确显示dict["title"]并使用dict["children"]作为子项。

<TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Value}">
       <HierarchicalDataTemplate.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}"/>
            </DataTemplate>
        </HierarchicalDataTemplate.ItemTemplate>
        <TextBlock Text="{Binding Key}"/>
    </HierarchicalDataTemplate>
</TreeView.ItemTemplate>

听说 Binding 支持 [] 语法,所以这样做似乎有用:

        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Path=[children]}">
                <HierarchicalDataTemplate.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=[title]}"/>
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
                <TextBlock Text="{Binding Path=[title]}"/>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>

每个字典都可以嵌套到任意层级,但是如何处理呢?使用上述模板和以下代码,无法显示孙子级别的内容。
        string s = @"[{""title"":""Title1"",""children"":[{""title"":""Child1"",""children"":[{""title"":""grandchild1"",""children"":[]}]}]}]";

        List<Dictionary<string, object>> marr = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(s);
        mTreeView.ItemsSource = marr;

顺便提一下,在绑定中使用[]被称为索引器,我从这个链接的文档中了解到:

http://msdn.microsoft.com/en-us/library/ms752300.aspx#Path_Syntax


你需要一个转换器,它可以从字典中获取/设置特定键的值。有了这样的转换器,您可以沿以下方式进行绑定:{Binding Converter={StaticResource GetFromDictionaryConverter}, ConverterParameter=children}{Binding Converter={StaticResource GetFromDictionaryConverter}, ConverterParameter=title},假设该转换器已添加为具有键GetFromDictionaryConverter的资源,并且它使用转换器参数作为键,并假定绑定源是一个Dictionary - odyss-jii
1个回答

1
这是您正在查看的东西吗:
<TreeView ItemsSource="{Binding ElementName=rootWindow, Path=Directories}">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding [Children]}">
                <TextBlock Text="{Binding [Title]}" />
                <HierarchicalDataTemplate.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}" />
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

这是我如何填充数据的方式:

Dictionary<string, object> documentsDictionary = new Dictionary<string, object>();
        List<string> documentsDictionaryChildren = new List<string> { "Document1", "Document2", "Document3", "Document4", "Document5" };
        documentsDictionary.Add("Title", "Documents");
        documentsDictionary.Add("Children", documentsDictionaryChildren);

        Dictionary<string, object> picturesDictionary = new Dictionary<string, object>();
        List<string> picturesDictionaryChildren = new List<string> { "Picture1", "Picture2", "Picture3", "Picture4", "Picture5" };
        picturesDictionary.Add("Title", "Pictures");
        picturesDictionary.Add("Children", picturesDictionaryChildren);

        Directories.Add(documentsDictionary);
        Directories.Add(picturesDictionary);

我在找到类似答案后刚刚编辑了问题。但是我的字典列表可以嵌套到任意级别。看起来你只有一个子级别。 - user317033
如果您的子节点也有子节点,那么您的模板是否能够正常工作呢? - user317033

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接