How to bind from a listItem template to a property in the list itself?

396 views Asked by At

I have a class (class A) that contains a list (list A) and and integer (int A). I also have a listview that its itemssource is bound to "list A". The listitems have a template that shows the itmes content (bound to its properties). I also want the template to use "int A" and show it.

how can I bind something inside the template (which its datacontext is listItem), to the class that contains the list (or even to the list itself) ?

thanks, Yos.

2

There are 2 answers

6
Christoph Fink On

If the List/Items do not have a reference to class A thats not possible, becuase the list could be refenrenced somewhere else too.
You can add something like a classA Owner {get;set;} property to your objects in the list and set this to class A whenever a item is added to the list (the CollectionChanged-event can handle that if you use an ObservableCollection).

0
brunnerh On

Usually that should be possible with a relative source binding that targets a control which still has the DataContext containing the property:

{Binding DataContext.ThatIntProperty,
         RelativeSource={RelativeSource AncestorType=ListView}}