在XAML中初始化DataTemplates的集合

时间:2017-03-28 11:54:53

标签: c# wpf xaml observablecollection

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

我需要什么DataType?或者我如何初始化ObservableCollection中的XAML

其他:

public class CustomWizardControl : Control {}

2 个答案:

答案 0 :(得分:1)

您的CustomWizardControl课程必须继承DepenedencyObject或其衍生类型之一,例如UIElementControl

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}" />

答案 1 :(得分:0)

您无法直接在XAML中设置ObservableCollection<T>的通用参数。

相反,您应该创建从DataTemplateCollection继承的自定义ObservableCollection<DataTemplate>。然后你就可以像往常一样使用你的收藏。

public class DataTemplateCollection : ObservableCollection<DataTemplate>
{
}

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

附加说明: NEVER 使用可变对象初始化依赖项属性的默认值,因为每个控件实例都将使用此单个可变实例。相反,您必须将默认值设置为null并在构造函数中确定初始值。