wpfxamlitemscontrolitemtemplateitemcontainergenerator

How to get control from ItemTemplate in ItemsControl?


My View has a collection of UserControls (defined in ItemsControl's ItemTemplate), and I want to get references to them.

I am using ItemContainerGenerator.ContainerFromIndex, but it's returning ContentPresenter, while I would need to get my UserControl type, PlotterColetaCanalUnico. How should I do that?

Xaml:

        <ItemsControl x:Name="plotter" ItemsSource="{Binding Sinais}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="1" IsItemsHost="True"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border x:Name="upper_light_border" BorderThickness="1,0,0,0" BorderBrush="#FFE5E5E5" SnapsToDevicePixels="True">
                        <Border x:Name="lower_dark_border" BorderThickness="0,0,0,1" BorderBrush="#FF1A1A1A" SnapsToDevicePixels="True">
                            <local:PlotterColetaCanalUnico/>
                        </Border>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

Code behind:

    IEnumerable<PlotterColetaCanalUnico> SubPlotters 
    {
        get
        {
            var plotters = new List<PlotterColetaCanalUnico>();

            for(int i = 0; i < plotter.Items.Count; i++)
            {
                var container = (UIElement)plotter
                                 .ItemContainerGenerator
                                 .ContainerFromIndex(i);

                // "container" ends up being ContentPresenter,
                // so the following cast does not work!
                var subPlotter = container as PlotterColetaCanalUnico;

                if (subPlotter != null)
                {
                    plotters.Add(subPlotter);
                }
            }

            return plotters;
        }
    }

I got it to work based on the accepted answer, and the following changes:

Xaml - added a name to the UserControl:

<local:PlotterColetaCanalUnico x:Name="plotterCanal"/>

Code behind - looked for the UserControl directly (without resorting to VisualTreeHelper as suggested by the answer):

                if (container == null)
                    continue;

                var template = container.ContentTemplate;
                var subPlotter = template.FindName("plotterCanal", container) as PlotterColetaCanalÚnico;

Solution

  • You need to dig into the visual tree a little more to find your control

    if (container != null)
    {
        var template = container.ContentTemplate;
        var border = template.FindName("upper_light_border", container) as Border;
        // From here, use VisualTreeHelper.GetChild to dig down in to the visual tree and find your control.
    
    }
    

    You can use this answer here to make a helper method for traversing the tree: https://stackoverflow.com/a/1759923/1231132