3

I have a UserControl consisting of some buttons and an ItemsControl. In the window using this control I want to set the ItemTemplate for this ItemsControl.

How can I achieve this?

UserControl:

<UserControl x:Class="WizardTest.WizardControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         x:Name="thisControl"
         Loaded="OnLoaded"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<DockPanel>
    <UniformGrid DockPanel.Dock="Bottom" Columns="4">
        <Button>Cancel</Button>
        <Button>&lt; Back</Button>
        <Button>Next &gt;</Button>
        <Button>Finish</Button>
    </UniformGrid>
    <ItemsControl DockPanel.Dock="Left" ItemsSource="{Binding WizardPages, ElementName=thisControl}">
    </ItemsControl>
</DockPanel>

Window:

<Window x:Class="WizardTest.EigenesWizardWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WizardTest"
    Title="Wizard Window" Height="300" Width="300">
<local:WizardControl WizardPages="{Binding WizardPages}">

</local:WizardControl>

MTR
  • 1,690
  • 3
  • 20
  • 47
  • 1
    How about creating an `ItemTemplate` property in your UserControl, which, when set, sets the embedded ItemControl's ItemTemplate property. – Clemens Jul 24 '13 at 09:50

2 Answers2

1

You need to give the control a name:

<ItemsControl x:Name="itemsControl" ...

Then in the code-behind for the UserControl you'd need to expose that ItemTemplate:

[BindableAttribute(true)]
public DataTemplate ItemTemplate
{
    get { return this.itemsControl.ItemTemplate; }
    set { this.itemsControl.ItemTemplate = value; }
}

And then finally you can use it:

<local:WizardControl ...>
    <local:WizardControl.ItemTemplate>
        <DataTemplate>
            ...
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
0

It should be as simple as this:

<DockPanel>
    ...
    <ItemsControl x:Name="itemsControl" ... />
</DockPanel>

and

public DataTemplate ItemTemplate
{
    get { return itemsControl.ItemTemplate; }
    set { itemsControl.ItemTemplate = value; }
}
Clemens
  • 123,504
  • 12
  • 155
  • 268