Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 每当我更改其中的项时,WPF ListView就会引发异常_C#_Wpf_Windows_Visual Studio_Listview - Fatal编程技术网

C# 每当我更改其中的项时,WPF ListView就会引发异常

C# 每当我更改其中的项时,WPF ListView就会引发异常,c#,wpf,windows,visual-studio,listview,C#,Wpf,Windows,Visual Studio,Listview,所以我已经有这个问题好几天了,我有一个简单的ListView项目,它会被动态填充。用户可以向列表中添加应显示在显示屏上的条目。但是,当我尝试对listView项执行任何操作时,无论是删除还是刷新它们,都会得到一个NullReferenceException。请看下面的代码 XAML文件(代码段): 下面是.cs代码的一个片段: public class Entry { public string documentName { get; set; } public

所以我已经有这个问题好几天了,我有一个简单的ListView项目,它会被动态填充。用户可以向列表中添加应显示在显示屏上的条目。但是,当我尝试对listView项执行任何操作时,无论是删除还是刷新它们,都会得到一个NullReferenceException。请看下面的代码

XAML文件(代码段):


下面是.cs代码的一个片段:

public class Entry {
  public string documentName {
    get;
    set;
  }

  public string languageAssistantName {
    get;
    set;
  }

  public string assignmentDate {
    get;
    set;
  }

  public string deadline {
    get;
    set;
  }

  public string progress {
    get;
    set;
  }

  public string supervisorName {
    get;
    set;
  }

  public string remarks {
    get;
    set;
  }

  public string proofread {
    get;
    set;
  }

  public Entry(string documentName, string assignmentDate, string deadline, string supervisorName, string remarks, string LAName, string progress, string proofread) {
    this.documentName = documentName;
    this.assignmentDate = assignmentDate;
    this.deadline = deadline;
    this.supervisorName = supervisorName;
    this.remarks = remarks;
    this.languageAssistantName = LAName;
    this.progress = progress;
    this.proofread = proofread;
  }
}

List < Entry > entryList = new List < Entry > ();

private void UpdateListView() //This is the code that adds the new entries to the listView, this works
{
  listLabel.Text = "";
  for (int i = 0; i < entryList.Count; i++) {
    listLabel.Text += i + " | " + entryList[i].ToString() + "\n\n";
    listView.Items.Insert(0, entryList[i]);
  }
}

/*This is the part that throws exception

*/
private void button_Click(object sender, RoutedEventArgs e) {
  listView.Items.RemoveAt(0);
}
公共类条目{
公共字符串文档名{
得到;
设置
}
公共字符串语言助手名称{
得到;
设置
}
公共字符串赋值日期{
得到;
设置
}
公共字符串截止日期{
得到;
设置
}
公共字符串进度{
得到;
设置
}
公共字符串管理器名称{
得到;
设置
}
公共字符串注释{
得到;
设置
}
公共字符串校对{
得到;
设置
}
公共条目(字符串文档名称、字符串分配日期、字符串截止日期、字符串主管名称、字符串备注、字符串名称、字符串进度、字符串校对){
this.documentName=documentName;
this.assignmentDate=assignmentDate;
this.deadline=截止日期;
this.supervisorName=supervisorName;
这个。备注=备注;
this.languageAssistantName=LAName;
这个。进步=进步;
这个。校对=校对;
}
}
列表entryList=新列表();
private void UpdateListView()//这是将新条目添加到listView的代码,可以使用
{
listLabel.Text=“”;
for(int i=0;i

我已经用SelectedItem、Refresh、ItemsSource和我能想到的所有东西尝试过了:/。每次尝试从列表中删除任何内容时,都会得到NullReferenceException。最糟糕的是,它实际上并没有显示错误发生的位置,所以我假设这是我无法控制的。但当我试图修复.NETFramework时,它说没有什么问题。有什么想法吗?

好吧,显然我是个白痴。我不知道,如果我在“发布”而不是“调试”中启动项目,VisualStudio实际上拒绝显示错误的位置。因此,在将其切换回去之后,我发现错误与listView完全无关

显然,在Entry类的“Equal”操作符中,p对象有时为null。现在我仍然不知道这是从哪里来的,但是在添加了一个额外的if语句之后,一切都运行得非常好


无论如何感谢您的帮助:)

只需将
ListView.ItemsSource
绑定到
observedcollection
并从集合中添加/删除项即可。你在这里重新发明了一个方盘。你的编码方式看起来像WinForms。如果您使用WPF,最好忘记它,而不要像使用WinForms那样使用它。看看WPF是如何利用数据绑定来呈现UI中的任何数据/内容/状态的……将其绑定到ObservableCollection实际上是我尝试的第一件事,也是同样的问题。我可以很好地添加它,但不允许我删除或更改任何内容。您是否显示任何数据?你能看到实际数据吗?你的意思是如果我知道我的列表中确实有数据吗?我知道列表工作正常,我可以打印所有内容,甚至listView.SelectedItem。
public class Entry {
  public string documentName {
    get;
    set;
  }

  public string languageAssistantName {
    get;
    set;
  }

  public string assignmentDate {
    get;
    set;
  }

  public string deadline {
    get;
    set;
  }

  public string progress {
    get;
    set;
  }

  public string supervisorName {
    get;
    set;
  }

  public string remarks {
    get;
    set;
  }

  public string proofread {
    get;
    set;
  }

  public Entry(string documentName, string assignmentDate, string deadline, string supervisorName, string remarks, string LAName, string progress, string proofread) {
    this.documentName = documentName;
    this.assignmentDate = assignmentDate;
    this.deadline = deadline;
    this.supervisorName = supervisorName;
    this.remarks = remarks;
    this.languageAssistantName = LAName;
    this.progress = progress;
    this.proofread = proofread;
  }
}

List < Entry > entryList = new List < Entry > ();

private void UpdateListView() //This is the code that adds the new entries to the listView, this works
{
  listLabel.Text = "";
  for (int i = 0; i < entryList.Count; i++) {
    listLabel.Text += i + " | " + entryList[i].ToString() + "\n\n";
    listView.Items.Insert(0, entryList[i]);
  }
}

/*This is the part that throws exception

*/
private void button_Click(object sender, RoutedEventArgs e) {
  listView.Items.RemoveAt(0);
}