将树视图绑定到字典< string,list< string>> [英] Bind treeview to dictionary<string,list<string>>

查看:64
本文介绍了将树视图绑定到字典< string,list< string>>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我制作了这样的字典:

public Dictionary<string, List<string>> sample { get; set; }


<TreeView Height="200" ItemsSource="{Binding sample }">
<TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Path=Value}"> 
                    <TextBlock FontWeight="Bold" Text="{Binding Path=Key}" /> 
                    <HierarchicalDataTemplate.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="xxx"/> 
                        </DataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
</TreeView>


I希望我的字典中的每个键都是标题,子项是列表中的值字符串。

I want to each key in my dictionary be a header and the children be the value strings in its list.

我尝试了人们在互联网上建议的所有模板,但是我的树视图没有显示任何内容!

I tried all templates people suggested in internet, but my treeview shows nothing!

现在我明白这是更新的问题,我的意思是当我更改这个词典时,即使我为这个字典改变了属性,也没有显示更改。

Now I understand it is the problem of update , I mean when I change this dictionary the changes are not showen even I call Property changed for this dictionary.

推荐答案



您可以通过属性绑定解决您的问题。而不是字符串使用具有Property的对象。

Hi,
you can solve your problem via property-binding. Instead of string use an object with Property.

sample = new Dictionary<string, List<Data>>();
...
      public class Data
      {
        public string Info { get; set; }
      }

这是一个演示:

XAML:

<Window x:Class="WpfApp1.Window07"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="Window07" Height="450" Width="800">
  <StackPanel>
    <TreeView Height="200" ItemsSource="{Binding sample }">
      <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Path=Value}">
          <TextBlock FontWeight="Bold" Text="{Binding Path=Key}" />
          <HierarchicalDataTemplate.ItemTemplate>
            <DataTemplate>
              <TextBlock Text="{Binding Info}"/>
            </DataTemplate>
          </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
      </TreeView.ItemTemplate>
    </TreeView>
  </StackPanel>
</Window>

代码:

using System.Collections.Generic;
using System.Windows;

namespace WpfApp1
{
  /// <summary>
  /// Interaction logic for Window07.xaml
  /// </summary>
  public partial class Window07 : Window
  {
    public Window07()
    {
      InitializeComponent();
      this.DataContext = new ViewModel();
    }
    public class ViewModel
    {
      public ViewModel()
      {
        sample = new Dictionary<string, List<Data>>();
        for (int i = 1; i < 10; i++)
        {
          List<Data> l = new List<Data>();
          for (int k = 1; k < 10; k++) l.Add(new Data() { Info =


" node {i} - {k} "}};
sample.Add(
"node {i}-{k}" }); sample.Add(


" sample {i}",l);
}
}
public Dictionary< string,List< Data>>样本{get;组; }
公共类数据
{
公共字符串信息{get;组; }
}
}
}
}
"sample {i}", l); } } public Dictionary<string, List<Data>> sample { get; set; } public class Data { public string Info { get; set; } } } } }


这篇关于将树视图绑定到字典&lt; string,list&lt; string&gt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆