简体   繁体   English

WPF:无法将值从目标保存回源

[英]WPF: Cannot save value from target back to source

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 我的SelectedSchoolclass更改,然后我加载相应的学生我的SelectedPupil更改,然后我加载相应的文档

PupilListView.xaml: 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. 在这里,ItemSource设置为SELECTED学校班级的所有学生。 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. 我不需要IsSynchronizedItem东西,因为由于新的延迟加载实体(仅通过父实体的ID),我实际上并没有聚合的数据。

AdminViewMOdel: 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. 当我从选定的文档或学生中跳出来并选择一个没有任何学生的学校课程时,它绑定到NULL,因为我的ObservableCollection是惰性创建的,意味着仅当我从数据库中获取数据时,否则PupilListViewModel_Collection为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. 好吧,我想坚持使用惰性加载,并且不需要像我以前用于急速加载那样的绑定层次结构,例如DataGrid_ItemsSource = SchoolclassList / PupilList。

How could I get rid of that exception? 我如何摆脱这种例外?

It seems like you should be able to return an empty list of the appropriate type instead of a NULL. 看来您应该能够返回适当类型的空列表,而不是NULL。 But, your post doesn't contain enough information for us to know that for sure. 但是,您的帖子所包含的信息不足,我们无法确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM