1

用户选择了一个包含文件的文件夹。我正在制作一个列表视图,显示所选文件夹中的文件。我想显示每个文件包含的内容,但我想在用户从 listviewitem 检查文件时显示它。我正在使用以下代码:

if (listView1.Items[0].Checked == true)
{
   //....
}

为什么它不起作用?例如,我应该使用什么数据:

button1.Click(...)button2.Click(...)

4

3 回答 3

4

不确定您要查找的确切内容,但有多种方法可以确定检查 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类来检查每个选定项目的详细信息。

于 2010-09-10T21:11:03.207 回答
1

你在捕捉哪个事件?请记住,如果它是,如果该项目是已选中/未选中的ItemCheck,则您不能使用。listView1.Item[0].Checked您需要获取ItemCheckEventArgs参数,并e.Index在检查整个列表视图元素时使用 , 排除该元素。用于e.NewValue单独评估引发ItemCheck事件的项目。

于 2010-09-10T23:50:06.087 回答
0

我会创建一个不错的 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) 等

如果我明白你说的话:)

于 2010-09-10T23:43:58.237 回答