WPF C#循环通过ListView选择的项目

时间:2015-06-03 13:37:57

标签: c# .net wpf listview

我有一个listview,需要遍历所选的项目集合。但我收到的错误是我的代码:

 DataRowView drv;
 foreach(var current in lstValue.SelectedItems)
 {
        ListBoxItem li  = new ListBoxItem();
        drv = current as DataRowView;
        li.Content = Criteria + "=" + drv["CODE"];
        lstSelection.Items.Add(li);
 }

我将错误对象引用设置为未设置为对象的实例。这是我已查找的堆栈溢出链接

C# WPF - Get the selected items from a ListView

2 个答案:

答案 0 :(得分:0)

你应该检查:

if( lstValue != null)
{
    foreach(var current in lstValue.SelectedItems)
    {
      // Your code
    }
}

答案 1 :(得分:0)

DataRowView drv;
foreach(var current in lstValue.SelectedItems)
{
    ListBoxItem li  = new ListBoxItem();
    drv = current as DataRowView;
    if (drv != null)
    {
        li.Content = Criteria + "=" + drv["CODE"];
        lstSelection.Items.Add(li);
    }
}