【发布时间】:2017-06-15 21:42:14
【问题描述】:
我有两个ListBoxes 和1 个Dictionary,当我从Listbox1 中选择一个键时,值将在ListBox2 中选择
Dictionary<string, string> f_list = new Dictionary<string, string>();
f_list.Add("First Shift", "Lauren");
f_list.Add("Second Shift", "Jamey");
f_list.Add("Third Shift", "Salem");
foreach (var t in f_list)
{
listBox1.Items.Add(t.Key);
listBox2.Items.Add(t.Value);
}
foreach (var t in f_list) {
if (listBox1.selecteditem == t.Key) {
}
}
【问题讨论】:
-
如果您想从
Listbox1中选择一个键并在ListBox2中选择值,那么您应该从Listbox1中获取键并从ListBox2中获取值 -
你能告诉我们你到目前为止的代码吗?
-
我不知道如何使用 'setselected()' 选择值
-
您需要提供有关您的 UI 的更多上下文。这是 WinForm 或 WPF 应用程序吗?如果是 WPF,你是在后面的代码中写的还是在 MVVM 后面写的?
-
我不知道第二个
foreach是干什么用的。您应该使用ListBox1的OnSelectedIndexChanged事件,并在该方法中查找其值,在f_list中找到相应的字符串,然后将该值分配给ListBox2。
标签: c# dictionary