C# 添加重复项的WPF集合依赖项属性

C# 添加重复项的WPF集合依赖项属性,c#,wpf,xaml,dependency-properties,C#,Wpf,Xaml,Dependency Properties,我创建了一个类,该类声明了一个附加属性,该属性将保存一组数据模板: public class DynamicTemplatesList : DependencyObject { public static readonly DependencyProperty TemplatesProperty = DependencyProperty.RegisterAttached("Templates", typeof(TemplateCollection), typeo

我创建了一个类,该类声明了一个附加属性,该属性将保存一组数据模板:

public class DynamicTemplatesList : DependencyObject
 {
    public static readonly DependencyProperty TemplatesProperty =
            DependencyProperty.RegisterAttached("Templates", typeof(TemplateCollection), typeof(DynamicTemplatesList), new FrameworkPropertyMetadata(new TemplateCollection(), 
    FrameworkPropertyMetadataOptions.None));

    public static void SetTemplates(UIElement element, TemplateCollection collection)
        {
            element.SetValue(TemplatesProperty, collection);
        }
}
然后在xaml中设置集合:

        <gfc:DynamicTemplatesList.Templates>
            <gfc:Template Key="{StaticResource CheckBoxFieldType}"
                                         DataTemplate="{StaticResource CheckBoxTemplate}" />
            <gfc:Template Key="{StaticResource LookupEditFieldType}"
                                         DataTemplate="{StaticResource LookupEditTemplate}" />
            <gfc:Template Key="{StaticResource TextBoxFieldType}"
                                         DataTemplate="{StaticResource TextBoxTemplate}" />
            <gfc:Template Key="{StaticResource DateEditFieldType}"
                                         DataTemplate="{StaticResource DateEditTemplate}" />
        </gfc:DynamicTemplatesList.Templates>

这似乎第一次就很好用。但是,我注意到,当我用这个依赖属性关闭窗口,然后重新打开它时, 似乎这些模板又被添加到了集合中

public class DynamicTemplatesList
{
    public static readonly DependencyProperty TemplatesProperty =
        DependencyProperty.RegisterAttached(
            "Templates",
            typeof(TemplateCollection),
            typeof(DynamicTemplatesList));

     public static TemplateCollection GetTemplates(UIElement element)
     {
         return (TemplateCollection)element.GetValue(TemplatesProperty);
     }

     public static void SetTemplates(UIElement element, TemplateCollection collection)
     {
         element.SetValue(TemplatesProperty, collection);
     }
}
第一次,集合中将有4个模板,第二次是8个,依此类推。有人能解释一下这里发生了什么吗


我怀疑这是因为依赖项属性的静态特性导致了值被复制,如果是这种情况,有人能告诉我一个解决方案来防止附加的集合属性添加重复项吗?

问题是您将
new TemplateCollection()
设置为依赖项属性的默认值。每当您使用属性而之前没有分配属性时,就会使用这个“静态”集合实例,就像您在XAML中所做的那样

对于包含可修改集合的依赖项属性的默认值,不应使用除
null
以外的任何内容

public class DynamicTemplatesList
{
    public static readonly DependencyProperty TemplatesProperty =
        DependencyProperty.RegisterAttached(
            "Templates",
            typeof(TemplateCollection),
            typeof(DynamicTemplatesList));

     public static TemplateCollection GetTemplates(UIElement element)
     {
         return (TemplateCollection)element.GetValue(TemplatesProperty);
     }

     public static void SetTemplates(UIElement element, TemplateCollection collection)
     {
         element.SetValue(TemplatesProperty, collection);
     }
}
现在在XAML中显式分配
TemplateCollection
实例,如下所示:

<gfc:DynamicTemplatesList.Templates>
    <gfc:TemplateCollection>
        <gfc:Template ... />
        <gfc:Template ... />
        <gfc:Template ... />
        <gfc:Template ... />
    </gfc:TemplateCollection>
</gfc:DynamicTemplatesList.Templates>



请注意,如果类只注册附加的属性,则不需要从
DependencyObject
派生。

问题在于您将
new TemplateCollection()
设置为dependency属性的默认值。每当您使用属性而之前没有分配属性时,就会使用这个“静态”集合实例,就像您在XAML中所做的那样

对于包含可修改集合的依赖项属性的默认值,不应使用除
null
以外的任何内容

public class DynamicTemplatesList
{
    public static readonly DependencyProperty TemplatesProperty =
        DependencyProperty.RegisterAttached(
            "Templates",
            typeof(TemplateCollection),
            typeof(DynamicTemplatesList));

     public static TemplateCollection GetTemplates(UIElement element)
     {
         return (TemplateCollection)element.GetValue(TemplatesProperty);
     }

     public static void SetTemplates(UIElement element, TemplateCollection collection)
     {
         element.SetValue(TemplatesProperty, collection);
     }
}
现在在XAML中显式分配
TemplateCollection
实例,如下所示:

<gfc:DynamicTemplatesList.Templates>
    <gfc:TemplateCollection>
        <gfc:Template ... />
        <gfc:Template ... />
        <gfc:Template ... />
        <gfc:Template ... />
    </gfc:TemplateCollection>
</gfc:DynamicTemplatesList.Templates>



请注意,如果类仅注册附加的属性,则不需要从
DependencyObject
派生。

如果我从new FrameworkPropertyMetadata(new TemplateCollection(),FrameworkPropertyMetadataOptions.None)更改dependency属性的默认值当我加载相关的xaml控件时,它会抛出一个null异常,声明Templates属性为null。另一件事是,我不需要从DependencyObject派生才能访问SetCurrentValue方法吗?是的,我确实更改了构造函数,以防混淆。我已经编辑了答案。您必须先将TemplateCollection的新实例分配给属性,然后才能使用它。我现在遇到的问题是,我似乎无法在DataTemplateSelector中检索这些模板:如果我从new FrameworkPropertyMetadata(new TemplateCollection(),FrameworkPropertyMetadataOptions.None)更改dependency属性的默认值当我加载相关的xaml控件时,它会抛出一个null异常,声明Templates属性为null。另一件事是,我不需要从DependencyObject派生才能访问SetCurrentValue方法吗?是的,我确实更改了构造函数,以防混淆。我已经编辑了答案。您必须先将TemplateCollection的新实例分配给属性,然后才能使用它。我现在遇到的问题是,我似乎无法在DataTemplateSelector中检索这些模板: