TreeView:绑定到分层数据

时间:2019-06-07 14:01:21

标签: c# wpf xaml treeview

我正在像这样声明的Dictionary中存储树状数据:

 Dictionary<string, object>

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

  1. 一个string
  2. 一个int
  3. 嵌套的Dictionary<string, object>

我正在尝试使用此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>

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

enter image description here

使用此数据:

Variable1...
  child1 = "hello"
  child2 = "there"
  child3...
    sub1 = "how"
    sub2 = "are"
    sub3 = "you"
Variable2...
  child1 = "lorem"
  child2 = "ipsum"

因此,它在子对象是stringint时起作用,但是在子对象是Dictionary时,它只是将其转换为字符串,而不进行递归处理。 / p>

如何获取这些数据?

编辑: 生成树的代码:

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;

1 个答案:

答案 0 :(得分:0)

您不能仅在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"/>

我的结果与您的数据一样

enter image description here