16

I need to be able to bind to a parent ItemsControl's properties from inside of a child ItemsControl data template:

<ItemsControl ItemsSource="{Binding Path=MyParentCollection, UpdateSourceTrigger=PropertyChanged}"> <ItemsControl.ItemTemplate> <DataTemplate> <ItemsControl ItemsSource="{Binding Path=MySubCollection}"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Path=MyParentCollection.Value, UpdateSourceTrigger=PropertyChanged}"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> 

Let's assume that MyParentCollection (the outer collection) is of the following type:

public class MyObject { public String Value { get; set; } public List<MyChildObject> MySubCollection { get; set; } 

And let's assume that MyChildObject from the above class is of the following type:

public class MyChildObject { public String Name { get; set; } } 

How can I bind to MyParentCollection.Value from inside of the inner data template? I can't really use a FindAncestor by type because they both all levels use the same types. I thought maybe I could put a name on the outer collection and use an ElementName tag in the inner binding, but that still couldn't resolve the property.

Any thoughts?

3
  • Can't you use FindAncestor mode, and specify both a Type and Ancestor level? Commented Jun 19, 2013 at 9:03
  • hmmm, didn't even think of ancestor level...I'll take a look at that Commented Jun 19, 2013 at 9:15
  • I can't seem to get that to work, oh well Commented Jun 22, 2013 at 7:14

2 Answers 2

25

saving the parent item in the tag of the child itemscontrol could work

 <DataTemplate> <ItemsControl ItemsSource="{Binding Path=MySubCollection}" Tag="{Binding .}"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Path=Tag.Value, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </DataTemplate> 

its not tested but give you a hint in the right direction :)

Sign up to request clarification or add additional context in comments.

2 Comments

Hack of the day, but nevertheless saved the day. Thank you very much! I hope that tagging binding simply makes a reference to the binding context and doesn't duplicate what's in the context.
Great hack of the day!
13

Binding for Tag, as suggested in the other answer, is not required. All data can be obtained from DataContext of ItemControl (and this markup Tag="{Binding}" just copies DataContext into Tag property, which is redundant).

<ItemsControl ItemsSource="{Binding Path=MyParentCollection}"> <ItemsControl.ItemTemplate> <DataTemplate> <ItemsControl ItemsSource="{Binding Path=MySubCollection}"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Path=DataContext.Value, RelativeSource={RelativeSource AncestorType=ItemsControl}}"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.