Search code examples
c#wpfxamlcaliburn

Iterations through more collections in XAML


I need to iterate through Shippers, then shipper.Parcels and finally use shipper.parcel.parcelNumber.

Application displays me only blank form (instead of generated buttons).

I am 100% sure, this error is caused only because of this XAML code. Please, does somebody see some mistake here?

Here is my whole XAML, the important part is described:

<Window x:Class="EnterEventTextBox.DataView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:EnterEventTextBox"
        mc:Ignorable="d" Background="Black"
        Title="DataView" Height="450" Width="300"
        xmlns:cal="http://www.caliburnproject.org"
        >
    <Window.Resources>
        <ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
            <WrapPanel MaxWidth="300" Orientation="Horizontal" IsItemsHost="True"/>
        </ItemsPanelTemplate>
        <Style x:Key="ItemsControlStyle1" TargetType="{x:Type ItemsControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ItemsControl}">
                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
                                Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                            <ScrollViewer CanContentScroll="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
                                <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </ScrollViewer>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>    

    <!-- HERE STARTS IMPORTANT CODE PIECE -->
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <ItemsControl ItemsSource="{Binding Shippers}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ItemsControl MaxWidth="300" MaxHeight="100" ItemsSource="{Binding Shipper_Parcels}" ItemsPanel="{DynamicResource ItemsPanelTemplate1}" 
                      Style="{DynamicResource ItemsControlStyle1}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate DataType="{x:Type Button}">
                                <Button Height="50" Width="50" Background="Red" Content="{Binding Shipper_Parcel_ParcelNumber}" Margin="0,0,5,5"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
    <!-- END -->
</Window>

Solution

  • I suspect you just are not taking into account that when displaying sub-elements the DataContext of those items are the individual items iterated in the parent.

    <ItemsControl ItemsSource="{Binding Shippers}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ItemsControl MaxWidth="300" MaxHeight="100" ItemsSource="{Binding Parcels}" ItemsPanel="{DynamicResource ItemsPanelTemplate1}" 
                      Style="{DynamicResource ItemsControlStyle1}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate DataType="{x:Type Button}">
                                <Button Height="50" Width="50" Background="Red" Content="{Binding ParcelNumber}" Margin="0,0,5,5"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    

    You should look at the output from the debugger as it'll output any bindings it can't figure out.