【问题标题】:TreeView: bind to hierarchical dataTreeView:绑定到分层数据
【发布时间】:2019-06-07 14:01:21
【问题描述】:

我将树状数据存储在 Dictionary 中,声明如下:

 Dictionary<string, object>

string 是一个标签,object 可以是以下之一:

  1. 一个string
  2. int
  3. 嵌套的Dictionary&lt;string, object&gt;

我正在尝试使用此 XAML 将其显示在 TreeView 中:

<TreeView Background="Black" Foreground="Yellow" ItemsSource="{Binding}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Path=Value}">
            <TextBlock Foreground="Red" Text="{Binding Path=Key}" />
            <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Foreground="LightGreen" Text="{Binding Path=Key}"/>
                        <TextBlock Foreground="White" Text="="/>
                        <TextBlock Foreground="Yellow" Text="{Binding Path=Value}"/>
                    </StackPanel>
                </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

这适用于顶级,但添加另一个级别如下所示:

有了这些数据:

变量 1... child1 =“你好” child2 =“那里” 孩子3... sub1 =“如何” sub2 = “是” sub3 =“你” 变量2... child1 = "lorem" child2 = "ipsum"

因此,当子对象为stringint 时,它可以工作,但当它是Dictionary 时,它只是将其转换为字符串,而不是递归处理它。

我怎样才能让这些数据显示出来?

编辑: 构建树的代码:

Dictionary<string, object> data = new Dictionary<string, object>();
Dictionary<string, object> child = new Dictionary<string, object>();
child["child1"] = "hello";
child["child2"] = "there";
Dictionary<string, object> child2 = new Dictionary<string, object>();
child2["sub1"] = "how";
child2["sub2"] = "are";
child2["sub3"] = "you";
child["child3"] = child2;
data["Variable1"] = child;

child = new Dictionary<string, object>();
child["child1"] = "lorem";
child["child2"] = "ipsum";
data["Variable2"] = child;

variablesWindow.DataContext = data;

【问题讨论】:

    标签: c# wpf xaml treeview


    【解决方案1】:

    你不能只在 xaml 中这样做,因为你不知道你的树有多深你必须设置一个递归方法来创建你的树。

    我的代码:

    public partial class MainPage : Window
    {
        private SolidColorBrush[] treeColors => new[]
        {
            Brushes.Red,
            Brushes.Green,
            Brushes.Yellow,
            Brushes.Purple,
            Brushes.Blue
        };
    
        public MainPage()
        {
            InitializeComponent();
            Dictionary<string, object> data = new Dictionary<string, object>();
            Dictionary<string, object> child = new Dictionary<string, object>();
            child["child1"] = "hello";
            child["child2"] = "there";
            Dictionary<string, object> child2 = new Dictionary<string, object>();
            child2["sub1"] = "how";
            child2["sub2"] = "are";
            child2["sub3"] = "you";
            child["child3"] = child2;
            data["Variable1"] = child;
    
            child = new Dictionary<string, object>();
            child["child1"] = "lorem";
            child["child2"] = "ipsum";
            data["Variable2"] = child;
    
            foreach (var item in data)
            {
                MyTreeView.Items.Add(CreateTreeViewItem(item.Value, item.Key));
            }
        }
    
        private object CreateTreeViewItem(object obj, string header, int deep = 0)
        {
            // Next color but don't make an out of range
            if (deep > treeColors.Length - 1) deep = treeColors.Length - 1;
    
            var item = new TreeViewItem()
            {
                Header = header,
                Foreground = treeColors[deep]
            };
    
            // Create a new tree view item
            if (obj is Dictionary<string, object> dic)
            {
                foreach (var o in dic)
                {
                    item.Items.Add(CreateTreeViewItem(o.Value, o.Key, deep + 1));
                }
            }
    
            // Write the "header = value"
            else
            {
                item.Header = new StackPanel()
                {
                    Orientation = Orientation.Horizontal,
                    Children =
                    {
                        new TextBlock
                        {
                            Text = header,
                            Foreground = treeColors[deep]
                        },
                        new TextBlock
                        {
                            Text = " = ",
                            Foreground = Brushes.White
                        },
                        new TextBlock
                        {
                            Text = obj.ToString(),
                            // Next color but don't make an out of range
                            Foreground = deep == treeColors.Length - 1? treeColors[deep]: treeColors[deep + 1]
                        },
                    }
                };
            }
    
            return item;
        }
    }
    

    还有我的小 xaml:

    <TreeView x:Name="MyTreeView" Background="Black" Foreground="Yellow"/>
    

    你的数据有我的结果

    【讨论】:

    • 谢谢。对 XAML 解决方案感到好奇,但看起来不可能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多