Reputation: 21216
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?
Upvotes: 1
Views: 5828
Reputation: 22717
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.
Upvotes: 0