I have two xamDataGrids one with Persons and one with Jobs and I'm using DataContext to bind to ObservableCollections Persons and Jobs (every person has a list with jobs). Here are my bindings:
DataSource="{Binding Path=Persons}"
DataSource="{Binding Path=Persons/Jobs}"
The problem I have is if I select a person in first grid it doesn't show the related jobs in the second grid, not even when using :
IsSynchronizedWithCurrentItem="True" .
Any ideas ?
In order for the IsSynchronizedWithCurrentItem property to work, you need to use a wrapper around your Persons object that implements ICollectionView.
This would work if you create a CollectionView from the Persons object like this:
ObservableCollection<Person> people = new ObservableCollection<Person>();CollectionView wrapper = new CollectionView(people);layout.DataContext = wrapper;
Let me know if you have any questions on this.
It doesn't work.
This is the modelI'm working with:
Firm
-> CollectionView(ObservableCollection<Person>) -> ObservableCollection<Jobs>
-> FirmDetail
and I'm setting
layout.DataContext = Firm;
I am attaching a working sample project with this. Please point out where is the difference between the two scenarios.
Thank you for your sample projects, seems to work fine. But getting closer to my model and using your Person & Jobs classes I've created a higher level class Firm:
public class Firm
{
private List<Person> people;
public Firm()
people = new List<Person>();
for (int i = 0; i < 5; i++)
Person p = new Person();
p.Name = "Name " + i;
List<Jobs> j = new List<Jobs>();
j.Add(new Jobs() { Title = p.Name + " Title" + i });
p.Jobs = j;
people.Add(p);
}
public CollectionView PersonsView { get { return new CollectionView(people); } set { } }
changed the Window constructor to :
public Window1()
InitializeComponent();
layout.DataContext = new Firm();
and the bindings to:
DataSource="{Binding Path=PersonsView, Mode=TwoWay}"
DataSource="{Binding PersonsView/Jobs}"
...bang! no more syncronization...why ?
Not sure but I guess that's because in your former pattern you were creating a NEW CollectionView each time you called PersonsView.
Interesting enough if I expose this from Firm class:
public List<Person> People { get { return people;} set { people = value;}}
public CollectionView PersonsView { get; set ; }
and change the constructor to this :
var firm = new Firm();
firm.PersonsView = new CollectionView(firm.People);
layout.DataContext = firm;
it actually works...go figure !