'WPF nested user controls getting error 26: ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's

First of all, I started from a week to studying WPF, so I will appologize for any mistakes I wrote here in code.

I have the following situation:

  • First user control made from a button, which contains some dependency properties, for a textblock's text and image source of an image object.

  • Second user control, contains a text and an ObservableCollection of first user control.

  • Third user control contains an ObservableCollection of second user control.

Each user control has its own model, with some properties, and implements INotifyPropertyChanged interface.

The xaml code for first user control:

<UserControl.Resources>
    <Style x:Key="RegButtonProperties" TargetType="local:RegistButton">
        <Setter Property="RegImageType" Value="{Binding RegImageTypeM}"/>
        <Setter Property="RegImage" Value="{Binding RegImageM}"/>
        <Setter Property="RegNumber" Value="{Binding RegNumberM}"/>
        <Setter Property="RegName" Value="{Binding RegNameM}"/>
        <Setter Property="ParentName" Value="{Binding ParentNameM}"/>
    </Style>
</UserControl.Resources>
<Button Style="{StaticResource ButtonStyle}" 
            Command="{Binding RegClick}" 
            CommandParameter="{Binding ElementName=RegButton, Path=RegName}"
            Cursor="Hand"
            Height="110"
            Width="60"
            Background="{StaticResource CustomDarkGrey30}">
        <Grid Margin="1">
            <Border BorderThickness="1" 
                    BorderBrush="AntiqueWhite"
                    />
            <Grid Background="{StaticResource CustomDarkGrey30}"
                  Opacity="0.5"
                  Margin="1"/>
            <Grid>
                <Image x:Name="RegButtonImage"
                       Margin="3"
                       Width="52"
                       Height="52"
                       Style="{StaticResource ImgStyle}"
                       Source="{Binding ElementName=RegButton, Path=RegImage}"
                       Opacity="1"
                       />
                <Grid VerticalAlignment="Top"
                      Height="{Binding ElementName=RegButtonImage, Path=Height}"
                      Margin="{Binding ElementName=RegButtonImage, Path=Margin}">
                    <TextBlock x:Name="RegNumberText"
                               FontFamily="{StaticResource DinotBold}"
                               Foreground="{StaticResource CustomDarkGrey80}"
                               FontWeight="Bold"
                               FontSize="12"
                               TextAlignment="Center"
                               VerticalAlignment="Center"
                               TextWrapping="Wrap"
                               Margin="{Binding ElementName=RegButtonImage, Path=Margin}"
                               Text="{Binding ElementName=RegButton, Path=RegNumber}"/>
                </Grid>
                <TextBlock x:Name="RegName"
                           Text="{Binding ElementName=RegButton, Path=RegName}"
                           FontFamily="{StaticResource DinotBold}"
                           FontSize="10"
                           Foreground="AntiqueWhite"
                           VerticalAlignment="Bottom"
                           TextWrapping="Wrap"
                           TextAlignment="Center"
                           Margin="2"
                           Height="46"
                           />
            </Grid>
        </Grid>
    </Button>

The xaml code for Second UC:

<UserControl.Resources>
        <Style x:Key="StackPanelStyle" TargetType="StackPanel">
            <Setter Property="Orientation" Value="Horizontal"/>
        </Style>
        <Style x:Key="Properties" TargetType="local:RBank">
            <!-- next line is the property of ObservableCollection<RegButton> -->
            <Setter Property="RegButtons" Value="{Binding RegButtonsM}"/> 
            <Setter Property="RegBankName" Value="{Binding RegBankNameM, TargetNullValue={x:Static system:String.Empty}}"/>
        </Style>
    </UserControl.Resources>
<Grid Height="Auto"
          Margin="0 5 0 0"
          >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0"
                   Foreground="AntiqueWhite"
                   Text="{Binding ElementName=RegBank, Path=RegBankName}"
                   HorizontalAlignment="Center"
                   FontFamily="{StaticResource DinotBold}"
                   FontSize="12"
                   />

        <ItemsControl ItemsSource="{Binding RegButtons}"
                      Grid.Row="1"
                      HorizontalAlignment="Center"
                      >
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel>
                        <StackPanel.Style>
                            <Style TargetType="StackPanel" BasedOn="{StaticResource StackPanelStyle}"/>
                        </StackPanel.Style>
                    </StackPanel>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <local:RegButton/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>

The third UC:

<UserControl.Resources>
        <Style x:Key="StackPanelStyle" TargetType="{x:Type StackPanel}">
            <Setter Property="Orientation" Value="Vertical"/>
        </Style>
        <Style TargetType="local:GContent">
            <Setter Property="RegBankCollection" Value="{Binding RegBankCollectionM}"/>
        </Style>
    </UserControl.Resources>
    <Grid Background="Transparent">
        <ItemsControl ItemsSource="{Binding Path=RegBankCollection}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel>
                        <StackPanel.Style>
                            <Style TargetType="StackPanel" 
                                   BasedOn="{StaticResource StackPanelStyle}"/>
                        </StackPanel.Style>
                    </StackPanel>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <local:RBank/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>

Main window xaml:

<!--region Content -->
    <Grid x:Name="Content"
          Grid.Row="2"
          Grid.ColumnSpan="3">
        <ContentControl x:Name="GContentControl"
                        Margin="3 0"
                        >
            <localViews:GContent x:Name="GContentView"/>
        </ContentControl>
    </Grid>
<!--endregion Content -->

When I click on a button in main window, the bellow code is running:

var gcm = new GContentModel();
gcm.PopulateGContent();
GContentView.DataContext = gcm; // here I receive the errors in debugger window
// and the only controls that appears on UI is the textblock from second control (RBank).

PopulateGContent method:

RegBankCollectionM = new()
            {
                new RBank() { RegBankName = "First reg bank" },
                new RBank() { RegBankName = "Second reg bank" },
                new RBank() { RegBankName = "Third reg bank" },
                new RBank() { RegBankName = "Fourth reg bank" }
            };
            foreach (var rb in RegBankCollectionM)
            {
                var r = new RBankModel();
                r.PopulateRegButtons();
                rb.DataContext = r;
            }

PopulateRegButtons:

public void PopulateRegButtons()
        {
            try
            {
                RegButtonsM = new();
                if (RegNumberM > 0)
                {
                    for (int i = 0; i < RegNumberM; i++)
                    {
                        var rbm = new RegButtonModel
                        {
                            ParentNameM = RegBankNameM,
                            RegNumberM = i + 1,
                            RegNameM = $"This is name for reg. {(i + 1)}"
                        };

                        RegButton btn = new()
                        {
                            DataContext = rbm
                        };
                        RegButtonsM.Add(btn);
                    }
                }
                OnPropertyChanged(nameof(RegButtonsM));
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Error creating reg bank: {ex.Message}");
            }
        }

what I see on UI:

enter image description here

errors in debugger: enter image description here

wpf


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source