Search code examples
c#wpfapp.xaml

Itemscontrol Modify control template


I want to modify itemscontrol template to modify not how each item apears but the actual itemscontrol

 <ItemsControl
         ItemsSource="{Binding RB}" Grid.Row="1"
         ItemTemplate="{StaticResource myBTemplate}">
         <ItemsControl.ItemsPanel>
                  <ItemsPanelTemplate>
                           <StackPanel></StackPanel>
                  </ItemsPanelTemplate>
         </ItemsControl.ItemsPanel>
 </ItemsControl>

I have this and it's works fine, I get a stackpanel behaviour of items and my custom data template for each item but I can't find a way to modify the container. Let's say to put the title into my items list without doing so outside itemscontrol. and if i do set ControlTemplate items no longer get displayed. (This Itemscontrol is part of an upper level DataTemplate)


Solution

  • After Clemens comment i searched more on Items presenter rather that ControlTemplate.

    Found i just needed to add items presenter somewhere in the ControlTemplate with no special tag just respecting tree format.

    <ItemsControl
      ItemsSource="{Binding RB}" Grid.Row="1"
      ItemTemplate="{StaticResource myBTemplate}">
             <ItemsControl.Template>
               <ControlTemplate TargetType="{x:Type ItemsControl}">
                 <StackPanel>
                  <Grid Margin="5" Height="50">
                   <Ellipse Stroke="DarkBlue" StrokeThickness="2">
                      <Ellipse.Fill>
                        <RadialGradientBrush Center="0.3,0.2" RadiusX="0.5" RadiusY="0.5">
                          <GradientStop Color="Azure" Offset="0.1" />
                          <GradientStop Color="CornflowerBlue" Offset="1.1" />
                        </RadialGradientBrush>
                      </Ellipse.Fill>
                   </Ellipse>
                  </Grid>
                   <StackPanel >
                     <ItemsPresenter Margin="2,0,0,0"/>
                   </StackPanel>
                 </StackPanel>
                </ControlTemplate>
               </ItemsControl.Template>
    </ItemsControl>