c# - WPF:将递归字典绑定(bind)到 TreeView

标签 c# wpf xaml dictionary treeview

我想使用 XAML 显示 Dictionary<string, object> 的 TreeView .每个字典的值要么是 string , 一个 Dictionary<string, object>Dictionary<string, object[]> .我找不到显示所有三种情况的 XAML。下面的 XAML 将显示一个漂亮的 TreeView,但终端节点(字符串)是空行。什么是正确的 XAML 还是我必须使用转换器?

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

最佳答案

您应该为 string 提供多个专门化的模板:

<TreeView xmlns:system="clr-namespace:System;assembly=mscorlib">
    <TreeView.Resources>
        <HierarchicalDataTemplate ItemsSource="{Binding Value}">
            <TextBlock Text="{Binding Key}"/>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type system:String}">
            <TextBlock Text="{Binding}" />
        </DataTemplate>
    </TreeView.Resources>
</TreeView>

关于c# - WPF:将递归字典绑定(bind)到 TreeView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25332496/

相关文章:

c# - 统一命名映射问题

c# - 如何将我的编程 WCF 配置转换为 app.config

wpf - 如何清除插件项目中的 WPF/XAML 类型缓存

wpf - 跨项目/dll 访问 Xaml 中的资源

c# - 需要更多地控制文本框的 MouseDown 和 KeyDown

c# - Windows 8.1 Metro 应用程序 - 捏合和缩放图像

c# - 使用 c# 以编程方式读取 Openoffice Calc (.ods)?

c# - 文件正忙。我不能删除它

c# - 使用具有不同类型详细信息的主从 View

.net - 绑定(bind) DataGrid 列标题