I’m creating a custom control called FooControl derived from ItemsControl have a default style defined for the same in themes\generic.xaml.
The default style for FooControl sets ItemsPanel property to another custom panel called FooPanel as below (I don’t think the usage of custom panel matters for this question).
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<local:FooPanel IsItemsHost="True"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
In the code behind of my FooControl, I want to get access to the instance of FooPanel that was created automatically. By looking in reflector I found that ItemsControl does have a property called ItemsHost but I cannot access it from FooControl as ItemsHost is internal
Can someone plz suggest me a reliable way to get reference to the instance of FooPanel?
Unfortunately the ItemsControl control does not expose a reference to this element (in fact, I have a feeling that the ItemsControl doesn’t even have access to it itself).
Using VisualTreeHelper.GetChild() as suggested in the previous post may work, but it will break if the items control is restyled in such a way that causes the items panel do be nested further down.
A slightly less fragile (but still far from perfect) way to get a reference is to get the parent of the container for the first child (assuming that your items control contains at least one item)
Note: This only works in SL3 and above since it uses the ItemContainerGenerator property (I believe a work around may exist for SL2)