views:

12

answers:

1

In this template ItemPresenter just defines host panel for the Items. Is it possible to define ItemTemplate?

<ControlTemplate x:Key="ItemsControlTemplate" TargetType="ItemsControl">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <ScrollViewer>
            <ItemsPresenter Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"/>
        </ScrollViewer>
    </Grid>
</ControlTemplate>

To go further, I've created a class:

public class ItemsControlExtended : ItemsControl
{
    public ItemsControlExtended()
    {
        DefaultStyleKey = typeof(ItemsControlExtended);
    }
}

And I would like to create a dependency property "ItemsMargin". After I've done that I'm supposed to bind an Item "Margin" property to "ItemsMargin". How it would be possible to implement?

GetContainerForItemOverride ? PrepareContainerForItemOverride ? OnApplyTemplate ?

+1  A: 

You can't define the ItemTemplate from with the ControlTemplate for the control as a whole.

Instead you would create a style that includes your control template and the the other templates as required:-

 <Style x:Key="ItemsControlStyle" TargetType="ItemsControl>
   <Setter Property="Template">
      <Setter.Value>
          <ControlTemplate TargetType="ItemsControl">
              <!-- your template as above -->
          </ControlTemplate>
      </Setter.Value>
   </Setter>

   <Setter Property="ItemsPanel">
      <Setter.Value>
          <ItemsPanelTemplate>
              <!-- An alternative to StackPanel if so desired -->
          </ItemsPanelTemplate>
      </Setter.Value>
   </Setter>


   <Setter Property="ItemTemplate">
      <Setter.Value>
          <DatalTemplate>
              <!-- The item template you wanted -->
          </DataTemplate>
      </Setter.Value>
   </Setter>

</Style>

Now you can style the ItemsControl:-

<ItemsControl Style="{StaticResource ItemsControlStyle}">
AnthonyWJones
Thank you, very helpful
Dmitry