如何在listBox中显示一个包含列表的字典C# [英] How to display a dictionary which contains a list in a listBox C#

查看:730
本文介绍了如何在listBox中显示一个包含列表的字典C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个字典,其中包含一个键值和与该键相关联的列表。
我已经阅读了如何将词典绑定到winforms中的ListBox ,当我尝试实现它只显示键值。



我想要做的是有两个单独的列表框。在方框1中,选择键值,发生这种情况时框2显示列表。当前代码如下:

  var xmlDoc2 = new XmlDocument(); 
xmlDoc2.Load(textBox1.Text);
字典< string,List< string>> dict = new Dictionary< string,List< string>>(StringComparer.OrdinalIgnoreCase);

var node = xmlDoc2.SelectNodes(pdml / packet / proto [@ name ='ip'] / @ showname);

foreach(节点中的XmlAttribute attribute1)
{
string ip = attribute1.Value;
var arr = ip.Split(); var src = arr [5]; var dst = arr [8];

列表< string>升;
if(!dict.TryGetValue(src,out l))
{
dict [src] = l = new List< string>();
}

l.Add(dst);

listBoxSRC.DataSource = new BindingSource(dict,null);
listBoxSRC.DisplayMember =Value;
listBoxSRC.ValueMember =Key;

}

到目前为止,这是显示listBoxSRC中的关键值,这很好。我需要做的是在listBoxDST中显示列表。



我还使用ListView来纠正这个问题,但我无法弄清楚它是如何工作的。 p>

我知道应该有一个listBoxSRC_SelectedIndexChange,但是我不断得到'dict不出现在这个上下文中'的错误。



谢谢

解决方案

我用一对列表框快速记下了一些事情。只需在表格中添加一对配对框,然后连线即可自行尝试。通过使用SelectedItem并将其转换为KeyValuePair,您不必在方法范围之外声明该字典,如下所示。

  public partial class Form1:Form 
{
public Form1()
{
InitializeComponent();

listBox1.DataSource = new BindingSource(new Dictionary< string,List< string>>
{
{四腿哺乳动物,新列表< string> {猫,狗,猪}},
{双腿哺乳动物,新列表< string> {人,黑猩猩,猿}}
}空值);

listBox1.DisplayMember =Value;
listBox1.ValueMember =Key;
}

private void listBox1_SelectedValueChanged(object sender,EventArgs e)
{
if(listBox1.SelectedItem!= null)
{
var keyValue =(KeyValuePair< string,List< String>)listBox1.SelectedItem;
listBox2.DataSource = keyValue.Value;
}
else
{
listBox2.DataSource = null;
}
}


I currently have a dictionary which contains a key value and a list associated with that key. I have read How to bind Dictionary to ListBox in winforms and when I try to implement that it just displays the key value.

What I am trying to do is have two separate listboxs. In box 1 you select the key value, when this happens box 2 displays the list. The current code is below:

var xmlDoc2 = new XmlDocument();
xmlDoc2.Load(textBox1.Text);
Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);

var node = xmlDoc2.SelectNodes("pdml/packet/proto[@name='ip']/@showname");

foreach (XmlAttribute attribute1 in node)
 {
   string ip = attribute1.Value;
   var arr = ip.Split(); var src = arr[5]; var dst = arr[8];

   List<string> l;
   if (!dict.TryGetValue(src, out l))
     {
        dict[src] = l = new List<string>();
     }

   l.Add(dst);

   listBoxSRC.DataSource = new BindingSource(dict, null);
   listBoxSRC.DisplayMember = "Value";
   listBoxSRC.ValueMember = "Key";

  }

What this does so far is displays the key value in listBoxSRC, which is fine. What I need to do is display the list in listBoxDST.

I also looked at using ListView to rectify this issue but I couldn't figure out how it worked.

I know there should be a listBoxSRC_SelectedIndexChange somewhere but I keep getting 'dict doesn't appear in this context' errors.

Thanks

解决方案

I jotted up something real quick with a pair of list boxes. Just make any form with a pair listboxes in it and wire up the event to try it yourself. By using the SelectedItem and casting it as the KeyValuePair that it is, you don't have to declare that dictionary outside of the method scope, as shown below.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        listBox1.DataSource = new BindingSource(new Dictionary<string, List<string>>
        {
            {"Four-Legged Mammals", new List<string>{"Cats", "Dogs", "Pigs"}},
            {"Two-Legged Mammals", new List<string>{"Humans", "Chimps", "Apes"}}
        }, null);

        listBox1.DisplayMember = "Value";
        listBox1.ValueMember = "Key";
    }

    private void listBox1_SelectedValueChanged(object sender, EventArgs e)
    {
        if (listBox1.SelectedItem != null)
        {
            var keyValue = (KeyValuePair<string, List<String>>) listBox1.SelectedItem;
            listBox2.DataSource = keyValue.Value;
        }
        else
        {
            listBox2.DataSource = null;
        }
    }

这篇关于如何在listBox中显示一个包含列表的字典C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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