WPF:无法将值从目标保存回源
这就是我收到的完整错误消息:
System.Windows.Data Error: 8 : Cannot save value from target back to source. BindingExpression:Path=SelectedPupil; DataItem='AdministrationViewModel' (HashCode=52357250); target element is 'DataGrid' (Name=''); target property is 'SelectedItem' (type 'Object') NullReferenceException:'System.NullReferenceException
当我从学生列表数据网格中选择的学生单击/选择到学校班级的列表框中时,总是会发生此错误。
当我将存储库加载技术从急切加载更改为延迟加载时,这种情况就开始发生。
我的 SelectedSchool 班级发生变化,然后我加载相应的学生 我的 SelectedPupil 发生更改,然后加载相应的文档
PupilListView.xaml:
<DataGrid
Grid.Row="1"
IsReadOnly="True"
HeadersVisibility="Column"
SelectedItem="{Binding SelectedPupil}"
ItemsSource="{Binding Path=SelectedSchoolclass.PupilListViewModel}"
AutoGenerateColumns="False"
>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding FirstName}" Width="*" Header="Firstname" />
<DataGridTextColumn Binding="{Binding LastName}" Width="*" Header="Last name" />
</DataGrid.Columns>
</DataGrid
这里的 ItemSource 设置为所选班级的所有学生。 我不需要 IsSynchronizedItem 的东西,因为我实际上没有聚合数据,因为新的延迟加载实体只是通过父实体的 id 来实现的。
AdminViewMOdel:
public PupilViewModel SelectedPupil
{
get { return _selectedPupil; }
set
{
_selectedPupil = value;
this.RaisePropertyChanged("SelectedPupil");
GetDocumentsForPupil();
}
}
private void GetDocumentsForPupil()
{
var documentsOC = new ObservableCollection<Document>(_docRepo.GetDocumentsByPupilId(_selectedPupil.Id));
SelectedPupil.Documents.DocumentList = documentsOC;
}
我想问题如下:
当我从选定的文档或学生跳转并选择没有任何学生的学校班级时,它绑定到 NULL,因为我的 ObservableCollection 是延迟创建的,意味着仅当我从数据库获取数据时,否则 PupilListViewModel_Collection 为 NULL。
好吧,我想坚持使用延迟加载,并且不需要像 DataGrid_ItemsSource=SchoolclassList/PupilList 这样的绑定层次结构,我用于急切加载。
我怎样才能摆脱这个异常?
Thats the full error message I get:
System.Windows.Data Error: 8 : Cannot save value from target back to source. BindingExpression:Path=SelectedPupil; DataItem='AdministrationViewModel' (HashCode=52357250); target element is 'DataGrid' (Name=''); target property is 'SelectedItem' (type 'Object') NullReferenceException:'System.NullReferenceException
This error always occurs when I click/select from my selected pupil in the pupilListDataGrid into the ListBox with the schoolclasses.
This started to occur when I changed my Repository loading technique from Eager loading to Lazy loading.
My SelectedSchoolclass changes then I load the according pupils
My SelectedPupil changes then I load the according documents
PupilListView.xaml:
<DataGrid
Grid.Row="1"
IsReadOnly="True"
HeadersVisibility="Column"
SelectedItem="{Binding SelectedPupil}"
ItemsSource="{Binding Path=SelectedSchoolclass.PupilListViewModel}"
AutoGenerateColumns="False"
>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding FirstName}" Width="*" Header="Firstname" />
<DataGridTextColumn Binding="{Binding LastName}" Width="*" Header="Last name" />
</DataGrid.Columns>
</DataGrid
Here the ItemSource is set to the all the pupils from the SELECTED schoolclass.
I do not need IsSynchronizedItem stuff because I do not really have aggregated data due to the new lazy loading entities just via id of the parent entity.
AdminViewMOdel:
public PupilViewModel SelectedPupil
{
get { return _selectedPupil; }
set
{
_selectedPupil = value;
this.RaisePropertyChanged("SelectedPupil");
GetDocumentsForPupil();
}
}
private void GetDocumentsForPupil()
{
var documentsOC = new ObservableCollection<Document>(_docRepo.GetDocumentsByPupilId(_selectedPupil.Id));
SelectedPupil.Documents.DocumentList = documentsOC;
}
I guess the problem is the following:
When I jump from a selected Document or Pupil and select a schoolclass without any pupils it binds to NULL because my ObservableCollection is lazily created means only when I get data from database else the PupilListViewModel_Collection is NULL.
Well I would like to stick with Lazy loading and do not need binding hierarchy like DataGrid_ItemsSource=SchoolclassList/PupilList what I used for Eager loading.
How could I get rid of that exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您应该能够返回适当类型的空列表而不是 NULL。但是,您的帖子没有包含足够的信息让我们确定这一点。
It seems like you should be able to return an empty list of the appropriate type instead of a NULL. But, your post doesn't contain enough information for us to know that for sure.