【问题标题】:Issue displaying dictionary list values in listbox C#在列表框 C# 中显示字典列表值的问题
【发布时间】:2014-10-17 22:25:10
【问题描述】:

我目前有两个列表框。一是存储密钥,二是查看与其关联的列表。

以下代码我在第一个 listBox 中显示了键,但未能在第二个中显示列表:

public void button1_Click(object sender, EventArgs e)
    {
        var xmlDoc = new XmlDocument();
        xmlDoc.Load(textBox1.Text);


        var node = xmlDoc.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];

            Dictionary<string, List<string>> dict = new Dictionary<string,List<string>>(StringComparer.OrdinalIgnoreCase);

            List<string> listDST;

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

            l.Add(listDST);

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

        }
    }

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

我已使用调试器检查以确保字典列表中包含数据,因此我不确定问题所在。

谁能指出我哪里出错了?

谢谢

【问题讨论】:

  • 试着用你自己的类来保存数据,ListView 显示你在 ToString() 中允许的内容

标签: c# winforms list dictionary listbox


【解决方案1】:

试试这个人

public void button1_Click(object sender, EventArgs e)
{
    var xmlDoc = new XmlDocument();
    xmlDoc.Load(textBox1.Text);


    var node = xmlDoc.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];

        Dictionary<string, List<string>> dict = new Dictionary<string,List<string>>(StringComparer.OrdinalIgnoreCase);

        List<string> listDST;

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

        l.Add(listDST);

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

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

【讨论】:

    【解决方案2】:

    “裸”字符串列表不能用作DataSource。您需要将它们包装在具有真实属性的简单类中。见here

    声明一个简单的字符串包装类之后:

    class aString { public string theString { get; set; } 
                    public aString(string s) { theString = s; }  
                    public override string ToString() {return theString;} }
    

    您可以更改您的字典以包含List&lt;aString&gt;,或者您可以从您的Dictionary Values 创建一个List&lt;aString&gt;

    List<aString> aStringList = dict [src].Select(item => new aString(item) ).ToList();
    listBoxDST.DataSource = aStringList ;
    

    ListBox 现在可以显示aString.ToString() 值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 2023-02-09
      • 2018-09-14
      相关资源
      最近更新 更多