I have a problem with Nested controls with ItemSources when changing an external DataContext control. Internal controls are updated to reflect the new DataContext, but this is similar to the "Ghost" binding, which is still bound to the old DataContext.
I suspect that having Nested controls that have DataTemplates prevents the internal control bindings from being updated when the external control of the DataContext changes. I read somewhere that only binding responds only to PropertyChanged events that arise from an object that is definitely defined in PATH.
My question is: how do you fully define the PATH binding from neighboring controls using ItemsSources? In my case:
<DataGrid name="OuterGrid" ItemsSource={Binding SelectedSchool.Classes}"> <ItemsControl ItemsSource={Binding Students}"> <ComboBox SelectedItem={Binding Grade}" /> </ItemsControl> </DataGrid>
I would like to completely specify the internal ComboBox SeletedItem PATH, something like the following, but I need it to bind to a specific element in the collection (and not just to index 0).
<ComboBox SelectedItem="{Binding ElementName=OuterGrid, Path=DataContext.SelectedSchool.Classes[0].Students[0].Grade}" />
I have a more detailed example of my problem below, I canβt post the actual code or describe the ACTUAL objects I work with (security reasons), so I tried to describe this in the easiest way to understand.
MODEL:
I have a rather complicated Biz object that has a collection of other objects. Items in the collection also have collections within them.
- There are many classes in schools.
- Classes have many students.
- Each student has a class for a class.
- The list of possible letter ratings for each school is different.
Each class (including my ViewModel) implements INotifyPropertyChanged, and each collection is an ObservableCollection.
ViewModel:
My ViewModel has the following features:
- An observable set of schools ... (AllSchools).
- Selected School
- Logical (IsEditing)
- An observable set of possible classes (which is updated when IsEditing changes and is based on the selected school).
It is important to note that different schools may have different possible classes (for example, someone has A +, A and A-, while the other only has A).
XAML:
I have a Datagrid binding on my ViewModel AllSchools collection and the SelectedSchool property of my ViewModel. When the user double-clicks the line, the event handler opens the editing panel for the selected school, changing the ViewModel IsEditing property (the Visibily editing panel is attached to the IsEditing property). Inside the editing panel I have a Datagrid (Bound to collection of Classes for the selected school), inside the Datagrid I have a TemplatedColumn with ItemsControl (binding to the collection from current class students). For each student, there is a ComboBox class for the student class in the classroom. The ItemsSource element for the ComboBox is a ViewModel PossibleGrades collection.
PROBLEM:
The problem is that when a SelectedSchool changes, any student in a previously selected school with a letter class that does not exist for the recently selected school suddenly has its own letter class equal to null (since the ItemsSource for ComboBox no longer has a grade).
Visually, everything is working fine. The editing panel correctly displays the properties of the selected school and is updated when the SelectedSchool property changes. But if I open the editing panel for the first school again, none of the drop-down lists will have the values ββselected more, because they were all set to zero when I selected the second school.
It, like the old ComboBoxes, still has Bindings, although they no longer appear on the screen. But if it affects only the previously selected school (and not on it before).