如何检查列表视图项目是否被选中

发布于 2024-09-19 02:02:54 字数 287 浏览 10 评论 0原文

用户选择一个包含文件的文件夹。我正在制作一个列表视图,显示所选文件夹中的文件。我想显示每个文件包含的内容,但我想在用户从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

メ斷腸人バ 2024-09-26 02:02:54

不确定您到底在寻找什么,但有多种方法可以确定检查 ListView 中的哪些项目:

// This loops through only the checked items in the ListView.
foreach (ListViewItem checkedItem in listView1.CheckedItems) {
    // All these ListViewItems are checked, do something...
}

// This loops through all the items in the ListView and tests if each is checked.
foreach (ListViewItem item in listView1.Items) {
    if (item.Checked) {
        // This ListViewItem is Checked, do something...
    }
}

您可以使用 ListViewItem 类,用于检查每个选定项目的详细信息。

Not sure exactly what you're looking for but there are a number ways to determine which items in a ListView are checked:

// This loops through only the checked items in the ListView.
foreach (ListViewItem checkedItem in listView1.CheckedItems) {
    // All these ListViewItems are checked, do something...
}

// This loops through all the items in the ListView and tests if each is checked.
foreach (ListViewItem item in listView1.Items) {
    if (item.Checked) {
        // This ListViewItem is Checked, do something...
    }
}

You can use the ListViewItem Class to examine the details of each selected item.

请持续率性 2024-09-26 02:02:54

您正在捕捉哪个事件?请记住,如果是 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 the listView1.Item[0].Checked if that item was what was checked/unchecked. You need to take the ItemCheckEventArgs parameter, and using the e.Index, exclude that element when checking the entire listview elements. Use e.NewValue to separately evaluate the item that raised the ItemCheck event.

云仙小弟 2024-09-26 02:02:54

我会创建一个漂亮的 MVVM 设计。 ViewModel 将有一个 ObservableCollection FileList,其中 File 将保存您想要的任何信息。这个类还有一个 IsFileSelectedUI 属性,以便您可以直接在代码中进行操作。那么在 XAML 中事情就很简单了:

<ScrollViewer Grid.Column="0" Grid.Row="1" >
<ItemsControl ItemsSource="{Binding FileList}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border BorderBrush="Gray" BorderThickness="1" Margin="2" Padding="2">
                <StackPanel Orientation="Horizontal">
                    <CheckBox IsChecked="{Binding IsFileSelectedUI , Mode=TwoWay}"/>
                    <TextBlock Text="{Binding FileName}"/>
                </StackPanel>
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

那么事情就这么简单:

FileList.Where(file=>file.IsFileSelectedUI)
等等

如果我明白你说的话:)

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:

<ScrollViewer Grid.Column="0" Grid.Row="1" >
<ItemsControl ItemsSource="{Binding FileList}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border BorderBrush="Gray" BorderThickness="1" Margin="2" Padding="2">
                <StackPanel Orientation="Horizontal">
                    <CheckBox IsChecked="{Binding IsFileSelectedUI , Mode=TwoWay}"/>
                    <TextBlock Text="{Binding FileName}"/>
                </StackPanel>
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Then things are just as easy as:

FileList.Where(file=>file.IsFileSelectedUI)
etc

If I understood what you said :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文