【问题标题】:Setting ItemTemplate of ItemsControl residing in UserControl设置驻留在 UserControl 中的 ItemsControl 的 ItemTemplate
【发布时间】:2013-07-24 09:46:34
【问题描述】:

我有一个由一些按钮和一个 ItemsControl 组成的 UserControl。在使用这个控件的窗口中,我想为这个 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>

【问题讨论】:

  • 如何在您的 UserControl 中创建一个 ItemTemplate 属性,该属性在设置时会设置嵌入式 ItemControl 的 ItemTemplate 属性。

标签: .net xaml user-controls


【解决方案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>
            ...

【讨论】:

    【解决方案2】:

    应该这么简单:

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

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

    【讨论】:

      猜你喜欢
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 2012-02-17
      • 2014-09-24
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      相关资源
      最近更新 更多