设置驻留在UserControl中的ItemsConmp的ItemTemplate

时间:2013-07-24 09:46:35

标签: .net xaml user-controls

我有一个UserControl,包含一些按钮和ItemsControl。在使用此控件的窗口中,我想为此ItemsControl设置ItemTemplate。

我怎样才能做到这一点?

用户控件:

<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 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>

2 个答案:

答案 0 :(得分:1)

您需要为控件命名:

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

然后在UserControl的代码隐藏中,您需要公开ItemTemplate

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

最后你可以使用它:

<local:WizardControl ...>
    <local:WizardControl.ItemTemplate>
        <DataTemplate>
            ...

答案 1 :(得分:0)

应该这么简单:

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

public DataTemplate ItemTemplate
{
    get { return itemsControl.ItemTemplate; }
    set { itemsControl.ItemTemplate = value; }
}