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# - 删除套接字超时后读取的所有待处理数据

c++ - freopen_s ("conout$") 和 fclose

windows - 64 位 Windows 上的 OpenSSL

node.js - npm升级后未构建Webpack

c# - 如何在 Win 10 UWP 项目中找到本地 IP 地址

c# - DataGridTextColumn 不允许输入 "double"值

c# - 人脸形状分割

c# - 在 WPF/XAML 中实现带有图标的工具栏

c# - 绑定(bind)命令的正确方法

c# - WPF 如何从 ViewModel 访问控件