Friday, 30 July 2010

Binding to ListData (with IsSynchronizedWithCurrentItem)

Today while testing out data binding in WPF to list controls, I learnt something new.

On the window, I created two list boxes, and a textblock control all have data context of an xml static resource.

For the ItemsSource in listbox I was using: {Binding XPath=@name} [the xml resource contains a Color element with attribute 'name]. I used IsSynchronizedWithCurrentItem on both the listboxes and bound the TextBlock Text to {Binding XPath=@Name}.

On testing the app I found, the controls are not synchronized. After referring to MSDN and some testing with different bindings - I found that to enable synchronization between all the controls to the CurrentItem of the collection, it is important to bind the list boxes to the whole collection. and not to a member or attribute (in this example).

So for listboxes it should be ItemsSource={Binding}, for display purposes we can use DisplayMemberPath or DataTemplate.


No comments:

Post a Comment

Note: only a member of this blog may post a comment.