我正在WinForms中构建一个简单的语言学习帮助器应用程序。其中一个模块是字典。它由“集合”组成,单词存储在一个集合中。用户可以创建一组新的单词并将其中的一部分存储在其中。WinForms ListView - 从选定的字符串项目获取源对象
我正在用列中的ListView
打印选定的一个或多个集合中的所有单词。在检查CheckedListBox
中的Set或Sets时,List清除并打印单词(作为字符串变量)。
当有几个检查他们的单词列表,并且用户想要编辑其中一个列出的单词时出现问题。我无法使用索引(例如列表项目的index
等于集合中的单词项目),因为这些列表字符串项目来自不同的集合。
有没有办法从ListView
项目获取源对象?我没有将对象添加到列表中,但仅添加了一些变量,但是它们以某种方式连接?
谢谢你的帮助。 干杯!
编辑:解释为什么我设置Tag
不是一个解决方案: 所有集都存储在一个List<DictionarySet> dictionarySets
。每个集合包含存储单词的List<Word> words
。
如何填充ListView控件:
private void UpdateList()
{
wordsListView.Items.Clear();
List<Word> currentSetWordList;
foreach (DictionarySet ds in setCheckedListBox.CheckedItems) //List<DictionarySets> dictionarySets inserted, DisplayMember set to its Name property
{
currentSetWordList = ds.words;
foreach (Word w in currentSetWordList)
{
ListViewItem newItem = new ListViewItem("--"); //for now a string, later an enum
newItem.Tag = ds;
newItem.SubItems.Add(w.GermanTranslation); //string property
newItem.SubItems.Add(w.PolishTranslation); //string property
wordsListView.Items.Add(newItem);
}
}
}
在这种情况下,程序遍历每个集及单词列表,并打印的话。他们的标签到处都是DictionarySet。
谢谢你的回答。我已经了解到,但它不能解决问题:每个Set都是一个DictionarySet对象,它们存储在一个List中。当我标记ListView项目时,它们都得到'DictionarySet'标签,所以它们没有区别。 –
Dandry