在XAML中初始化DataTemplates的集合

在XAML中初始化DataTemplates的集合

本文介绍了在XAML中初始化DataTemplates的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个DependencyProperty

public ObservableCollection<DataTemplate> WizardTemplateCollection
{
    get { return (ObservableCollection<DataTemplate>)GetValue(WizardTemplateCollectionProperty); }
    set { SetValue(WizardTemplateCollectionProperty, value); }
}

// Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty WizardTemplateCollectionProperty =
    DependencyProperty.Register("WizardTemplateCollection", typeof(ObservableCollection<DataTemplate>), typeof(CustomWizardControl), new PropertyMetadata(new ObservableCollection<DataTemplate>()));

并希望这样做:

<custom:CustomWizardControl>
    <custom:CustomWizardControl.WizardTemplateCollection>
        <DataTemplate>
            <Rectangle></Rectangle>
        </DataTemplate>
        <DataTemplate>
            <Rectangle></Rectangle>
        </DataTemplate>
        <DataTemplate>
            <Rectangle></Rectangle>
        </DataTemplate>
    </custom:CustomWizardControl.WizardTemplateCollection>
</custom:CustomWizardControl>

我需要什么数据类型?或如何在XAML中初始化ObservableCollection.

What DataType do i need? Or how can i initialize a ObservableCollection in XAML.

其他:

public class CustomWizardControl : Control {}

推荐答案

您的CustomWizardControl类必须继承DepenedencyObject或其派生类型之一,例如UIElementControl:

Your CustomWizardControl class must inherit from DepenedencyObject or one of its derived types like for example UIElement or Control:

public class CustomWizardControl : Control
{
    public ObservableCollection<DataTemplate> WizardTemplateCollection
    {
        get { return (ObservableCollection<DataTemplate>)GetValue(WizardTemplateCollectionProperty); }
        set { SetValue(WizardTemplateCollectionProperty, value); }
    }
    ...
}

这有效:

public class CustomWizardControl : Control
{
    public CustomWizardControl()
    {
        WizardTemplateCollection = new ObservableCollection<DataTemplate>();
    }

    public ObservableCollection<DataTemplate> WizardTemplateCollection
    {
        get { return (ObservableCollection<DataTemplate>)GetValue(WizardTemplateCollectionProperty); }
        set { SetValue(WizardTemplateCollectionProperty, value); }
    }

    public static readonly DependencyProperty WizardTemplateCollectionProperty =
        DependencyProperty.Register("WizardTemplateCollection", typeof(ObservableCollection<DataTemplate>), typeof(CustomWizardControl), new PropertyMetadata(null));
}


<local:CustomWizardControl x:Name="ctrl">
    <local:CustomWizardControl.WizardTemplateCollection>
        <DataTemplate>
            <Rectangle></Rectangle>
        </DataTemplate>
        <DataTemplate>
            <Rectangle></Rectangle>
        </DataTemplate>
        <DataTemplate>
            <Rectangle></Rectangle>
        </DataTemplate>
    </local:CustomWizardControl.WizardTemplateCollection>
</local:CustomWizardControl>


<TextBlock Text="{Binding WizardTemplateCollection.Count, ElementName=ctrl}" />

这篇关于在XAML中初始化DataTemplates的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 23:40