我有一个树形视图的ObservableCollection,当我试图从ObservableCollection中删除一个项目时,我会得到一个错误,那就是对象引用没有设置为对象的实例。您能告诉我为什么会发生这个错误吗?解决方案是什么?
谢谢
编辑1:代码类似于:
class MyClass : INotifyPropertyChanged
{
//my class code here
}
public partial class UC_myUserControl : UserControl
{
private ObservableCollection<MyClass> myCollection = new ObservableCollection<MyClass>();
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
myCollection.add(new myClass);
myTreeView.DataContext = myCollection ;
}
private void deleteItem()
{
myCollection.RemoveAt(0);
//after removing I get error Which I guess should be something related
//to interface update but I don't know how can I solve it
}
}异常详细信息: System.NullReferenceException未处理Message=“对象引用未设置为对象的实例。”Source="PresentationFramework“
编辑3:我有一个样式,用于我的树项目,以保持树项目的展开
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True" />
</Style>通过注释这部分,我不会得到任何错误!现在我想把我的问题改成为什么使用这种风格会导致错误?
发布于 2010-06-17 23:11:04
正如我在EDIT2中所说的,原因是我使用的样式展开了所有的树项目,去掉了样式,所有评论的isue都解决了.thanks。
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True" />
</Style>发布于 2010-06-21 16:37:09
当我还绑定到集合中的选定项并尝试删除选定项时,我也遇到了类似的问题。我必须首先更改选定的项目,然后将其删除。
https://stackoverflow.com/questions/3062287
复制相似问题