【问题标题】:WPF C# loop through ListView selected itemsWPF C# 循环遍历 ListView 所选项目
【发布时间】:2015-06-03 13:37:56
【问题描述】:

我有一个列表视图,需要遍历选定的项目集合。但我收到以下错误是我的代码:

 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

【问题讨论】:

  • 哪一行报错了?
  • 我的 datarowview 是 null 。 drv 没有被填满。我做得对吗??
  • 它只是一个带有代码和描述的多列项目。理想情况下,它应该遍历所选项目并让我获得所有代码
  • 正如 Blam 所说,哪一行会抛出错误?

标签: c# .net wpf listview


【解决方案1】:

你应该检查:

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

【讨论】:

    【解决方案2】:
    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);
        }
    }
    

    【讨论】:

    • 我的 DataRowView 没有被填满,它不为空
    猜你喜欢
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-30
    相关资源
    最近更新 更多