Reputation: 7451
What is the proper way to bind to the Parent of an ItemsControl from within the ItemsControl.ItemTemplate?
Non working attempt:
<ControlTemplate TargetType="{x:Type local:ParentUserControl}">
<ItemsControl ItemsSource="{Binding MyCollectionViewSource.View, RelativeSource={RelativeSource TemplatedParent}}"
IsTabStop="False"
Focusable="False">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:ChildUserControl BoundProp1="{Binding Prop1}"
BoundObjProp2="{Binding RelativeSource={RelativeSource
Mode=FindAncestor,
AncestorType={x:Type local:ParentUserControl}}}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ControlTemplate>
Upvotes: 0
Views: 1891
Reputation: 5948
I had similar requirement and the following worked for me:
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" ItemWidth="{Binding ItemWidth}" ItemHeight="{Binding ItemHeight}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate >
<views:MyUserControl
Width="{Binding DataContext.ItemWidth, ElementName=PageRoot}"
Height="{Binding DataContext.ItemHeight, ElementName=PageRoot}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
P.S. my app is in XAML for WinRT
Upvotes: 1
Reputation: 7451
I have not found a solution to this problem. In the end I had to use work arounds which violated my desired separation of concerns, but functioned as expected. I believe this comes down to an issue in the wpf framework, hopefully 4.5 will fix it.
Upvotes: 0
Reputation: 185350
The binding looks fine to me, but you do not specify a Binding.Path
, are you sure that you want to bind directly to the control and not a property?
Upvotes: 1