如何检查列表视图项目是否被选中
用户选择一个包含文件的文件夹。我正在制作一个列表视图,显示所选文件夹中的文件。我想显示每个文件包含的内容,但我想在用户从 listviewitem 检查文件时显示它。我正在使用以下代码:
if (listView1.Items[0].Checked == true)
{
//....
}
为什么它不起作用?我应该使用什么数据,例如:
button1.Click(...)
到 button2.Click(...)
?
User chose a folder containing files. I'm making a listview displaying the files in the chosen folder. I want to display what each file contains, but i want to display it when the user checks a file from listviewitem. I'm using the following code:
if (listView1.Items[0].Checked == true)
{
//....
}
Why doesn't it work? What should i want to use data from for example:
button1.Click(...)
to button2.Click(...)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不确定您到底在寻找什么,但有多种方法可以确定检查 ListView 中的哪些项目:
您可以使用 ListViewItem 类,用于检查每个选定项目的详细信息。
Not sure exactly what you're looking for but there are a number ways to determine which items in a ListView are checked:
You can use the ListViewItem Class to examine the details of each selected item.
您正在捕捉哪个事件?请记住,如果是
ItemCheck
,则如果该项目是已选中/未选中的项目,则不能使用listView1.Item[0].Checked
。您需要采用ItemCheckEventArgs
参数,并使用e.Index
在检查整个列表视图元素时排除该元素。使用e.NewValue
单独评估引发ItemCheck
事件的项目。Which event are you capturing? Remember if it's the
ItemCheck
, that you cannot use thelistView1.Item[0].Checked
if that item was what was checked/unchecked. You need to take theItemCheckEventArgs
parameter, and using thee.Index
, exclude that element when checking the entire listview elements. Usee.NewValue
to separately evaluate the item that raised theItemCheck
event.我会创建一个漂亮的 MVVM 设计。 ViewModel 将有一个 ObservableCollection FileList,其中 File 将保存您想要的任何信息。这个类还有一个 IsFileSelectedUI 属性,以便您可以直接在代码中进行操作。那么在 XAML 中事情就很简单了:
那么事情就这么简单:
如果我明白你说的话:)
I would create a nice MVVM design. The ViewModel would have an ObservableCollection FileList, where File would hold whatever information you want. This class would have also an IsFileSelectedUI property so that you could right in your code. Then in XAML things are easy:
Then things are just as easy as:
If I understood what you said :)