views:

29

answers:

1

I have a Silverlight user control that contains an ItemsControl that renders a StackPanel containing another user control for each item in the data source, the XAML is as follows:

<Grid x:Name="LayoutRoot">
    <ItemsControl ItemsSource="{Binding}" x:Name="ValuesItemSource">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel x:Name="ValuesPanel" Background="Transparent" Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <controls:MyCustomControl DataContext="{Binding}"  x:Name="Value" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

How would I reference the collection of MyCustomControls (*Value*s) in the code behind of this user control?

(I have an event handler registered in the code behind of this control and I want to invoke a method of each "MyCustomControl" when the event fires)

+1  A: 

You need to ask the itemsControl.ItemContainerGenerator for this. See here for an example.

David Lynch
Thanks David. Your comment led me to http://stackoverflow.com/questions/980120/finding-control-within-wpf-itemscontrol which is exactly what I was after.
kristian