c# – WPF – Use ItemsControl inside ItemsControl

Question:

I am having a somewhat strange problem. To start I have these two classes:

public class Server
{
    public Guid id { get; set; }
    public string descripcion { get; set; }
    public string nombre { get; set; }
    public List<PathSync> carpetas { get; set; }
}

public class PathSync
{
    public Guid id { get; set; }
    public string origen { get; set; }
    public string destino { get; set; }
    public int tipocopia { get; set; }
}

In my application I create a list of Server objects and initialize them, also creating the list of PathSync objects within each object. Once I have this list created, I initialize the ItemsControls with collapsedPanelItem.ItemsSource = lista .

Then, in my WPF application, in the view, I have the following block:

<ItemsControl x:Name="collapsedPanelItem">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Expander Header="{Binding nombre}">
                <StackPanel Margin="10" Orientation="Vertical">
                    <TextBlock TextWrapping="Wrap" Text="{Binding descripcion}" />
                    <ItemsControl ItemsSource="{Binding carpetas}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Vertical">
                                    <Label Content="{Binding origen}" />
                                    <Label Content="{Binding destino}" />
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>
            </Expander>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

When I run my application, the drop-down list appears showing the name and, when I display it, I see the label inside with the description, but the second ItemsControl does not show the list of folders.

Does anyone know what I'm doing wrong?

Answer:

You need to place the data type in your DataTemplate, the x: Type consists of two parts (xlmns reference: class name):

You must refer in your namespace of your UserControl, Page or whatever you are using, these lines are in the initial part of your xaml file:

Example:

xmlns:local="clr-namespace:Project.ServerElements"

once referenced, you put it in your DataTemplate like this:

<ItemsControl x:Name="collapsedPanelItem">
    <ItemsControl.ItemTemplate>
        <DataTemplate DataType="{x:Type local:Server}">
            <Expander Header="{Binding nombre}">
                <StackPanel Margin="10" Orientation="Vertical">
                    <TextBlock TextWrapping="Wrap" Text="{Binding descripcion}" />
                    <ItemsControl ItemsSource="{Binding carpetas}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Vertical">
                                    <Label Content="{Binding origen}" />
                                    <Label Content="{Binding destino}" />
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>
            </Expander>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Hope this can help you.

Greetings

Scroll to Top