【发布时间】: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