c# - xaml 中带有集合的嵌套对象似乎会泄漏到相邻的集合中

标签 c# wpf windows xaml

当我定义一个对象集合时,每个对象都包含这样一个集合:

<Graph2:NestedObjectTest x:Key="nestedTest">
    <Graph2:NestedObjectTest.Children>
        <Graph2:ChildNestedObjectTest>
            <Graph2:ChildNestedObjectTest.Children>
                <Point X="0" Y="0"/>
                <Point X="10" Y="10"/>
                <Point X="20" Y="20"/>
            </Graph2:ChildNestedObjectTest.Children>
        </Graph2:ChildNestedObjectTest>
        <Graph2:ChildNestedObjectTest>
            <Graph2:ChildNestedObjectTest.Children>
                <Point X="1" Y="0"/>
                <Point X="11" Y="10"/>
                <Point X="21" Y="20"/>
            </Graph2:ChildNestedObjectTest.Children>
        </Graph2:ChildNestedObjectTest>
        <Graph2:ChildNestedObjectTest>
            <Graph2:ChildNestedObjectTest.Children>
                <Point X="2" Y="0"/>
                <Point X="12" Y="10"/>
                <Point X="22" Y="20"/>
            </Graph2:ChildNestedObjectTest.Children>
        </Graph2:ChildNestedObjectTest>
    </Graph2:NestedObjectTest.Children>
</Graph2:NestedObjectTest>

每个 ChildNestedObjectTest 对象都获得所有九个 Point 对象,而不是每个对象都获得它们看起来应该拥有的三个对象。 这是为什么?

后面的代码是:

namespace Graph2
{
    public class NestedObjectTest : DependencyObject
    {
        public static readonly DependencyProperty ChildrenProperty = DependencyProperty.Register("Children", typeof(List<ChildNestedObjectTest>), typeof(NestedObjectTest), new FrameworkPropertyMetadata(new List<ChildNestedObjectTest>()));
        public List<ChildNestedObjectTest> Children
        {
            get { return (List<ChildNestedObjectTest>)GetValue(ChildrenProperty); }
            set { SetValue(ChildrenProperty, value); }
        }
    }
    public class ChildNestedObjectTest : DependencyObject
    {
        public static readonly DependencyProperty ChildrenProperty = DependencyProperty.Register("Children", typeof(List<Point>), typeof(ChildNestedObjectTest), new FrameworkPropertyMetadata(new List<Point>()));
        public List<Point> Children
        {
            get { return (List<Point>)GetValue(ChildrenProperty); }
            set { SetValue(ChildrenProperty, value); }
        }
    }
}

我通过将 xaml 作为资源添加到窗口进行测试,然后使用:

Graph2.NestedObjectTest test = (Graph2.NestedObjectTest)this.Resources["nestedTest"];

最佳答案

Collection-Type Dependency Properties

总结

If your property is a reference type, the default value specified in dependency property metadata is not a default value per instance; instead it is a default value that applies to all instances of the type.

看起来像您在元数据中设置的默认静态值

 new FrameworkPropertyMetadata(new List<Point>(),
                                          ^
                                          ---- This

无论何时都被重新使用,封闭的泛型是另一个 DependencyObject/Freezable

修复

To correct this problem, you must reset the collection dependency property value to a unique instance, as part of the class constructor call. Because the property is a read-only dependency property, you use the SetValue(DependencyPropertyKey, Object) method to set it, using the DependencyPropertyKey that is only accessible within the class.

public NestedObjectTest() : base()
{
    SetValue(ChildrenProperty, new List<ChildNestedObjectTest>()); 
}

public ChildNestedObjectTest() : base()
{
    SetValue(ChildrenProperty, new List<Point>()); 
}

关于c# - xaml 中带有集合的嵌套对象似乎会泄漏到相邻的集合中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29267168/

相关文章:

c#正则表达式查找并提取给定长度的数字

wpf - 实现诸如MVC或MVVM的架构模式是否为时已晚?

wpf - 将IValueConverter与DynamicResource一起使用?

c# - 在 EF Core 2 中使用单一表名

c# - 如何获取完全加载的 HTML 页面代码

windows - 如果网络适配器已重置,如何保持 TCP 套接字连接

c++ - MFC中如何在资源链中查找资源?

c++ - 更新进程地址空间中的 HTML 内容

c# - 在调用 Add() 之后但在调用 SaveChanges() 之前从上下文中检索实体

C# WPF Caliburn Micro TreeViewItem.Expanded 事件未触发