使用文本标签和键值将项添加到列表视图

Luk*_*uke 3 c# listview

我正在尝试向ListView控件添加项目.我希望添加带有文本值(显示)的项目以及选中它时的隐藏键值.

我试过以下代码:

string flows_path = "C:\\temp\\Failed Electricity flows\\";
            List<ListViewItem> flows_loaded = new List<ListViewItem>();

            foreach (string s in Directory.GetFiles(flows_path, "*.rcv").Select(Path.GetFileName))
            {
                ListViewItem new_item = new ListViewItem(s, 1);
                ListViewItem.ad

                // Add the flow names to the list
                flows_loaded.Add(new_item);

            }
Run Code Online (Sandbox Code Playgroud)

但它告诉我ListViewItem没有重载,(string, int)它似乎没有我可以设置的'值','文本'或'键'值.

ListViewItem("My Item") 有效,但我不知道如何为每个项目实现一个密钥.

Joh*_*mse 8

您可以通过将其存储在Tag属性中来存储与ListViewItem关联的其他值.

ListViewItem new_item = new ListViewItem(s);
new_item.Tag = my_key_value;
Run Code Online (Sandbox Code Playgroud)

ETA:请记住Tag属性是类型的object,因此在某些情况下,您可能需要在检索值时显式地将值转换为正确的类型.