2010-09-10 50 views
0

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

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

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

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

+1

哪个“ListView”? Web窗体? Windows窗体? WPF? SilverLight的? – 2010-09-10 20:56:21

+1

也考虑'如果(listView1.Items [0] .Checked)' – 2010-09-10 20:56:42

+1

luc,当发布一个问题时,它有助于包括错误消息或至少更多的细节,至于什么“不工作” – NotMe 2010-09-10 20:56:47

回答

1

哪个ev你在捕捉?请记住,如果它是ItemCheck,那么如果该项目被选中/未选中,则不能使用listView1.Item[0].Checked。您需要采取ItemCheckEventArgs参数,并使用e.Index,检查整个listview元素时排除该元素。使用e.NewValue分别评估引发ItemCheck事件的项目。

3

不知道你寻找什么,但也有一些方法来确定其在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类检查每个选项的细节。

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.IsFileSelectedUI) 等

如果我理解你说的话:)

+0

这不是一个错误的WPF设计,但它不回答OP的问题,这是您如何确定.NET Framework ListView中的项目是否被选中的问题... – 2010-09-10 23:48:37

相关问题