gpt4 book ai didi

wpf - 自定义控件依赖属性集合,统计嵌套控件项

转载 作者:行者123 更新时间:2023-12-04 04:35:57 25 4
gpt4 key购买 nike

我创建了两个自定义控件。 1. 一个 LeafControl 2- LeafItemControl。
在 LeafControl 中,我创建了一个“列表”类型的依赖属性作为“项目”。

  • 同样在 LeafItemControl 中,我公开了另一个 ContentControl 类型的依赖属性调用“ItemDetails”。
    <!---Base Custom Control "LeafControl"-->












         <uc:LeafControlItem.ItemDetails> <!--"ItemDetails" is a Dependency Property of type "LeafControl" in "LeafControlItem" custom control -->
    <uc:LeafControl> <!--Nested Control of same type ???-->
    <uc:LeafControl.Items>
    <uc:LeafControlItem Level="Some Type">
    <uc:LeafControlItem.ItemContent>
    <GroupBox BorderThickness="0">
    <StackPanel Orientation="Horizontal">
    <TextBox Text="Property"></TextBox>
    </StackPanel>
    </GroupBox>
    </uc:LeafControlItem.ItemContent>

    </uc:LeafControlItem>
    <uc:LeafControlItem Level="Variable">
    <uc:LeafControlItem.ItemContent>
    <GroupBox BorderThickness="0">
    <StackPanel Orientation="Horizontal">
    <TextBox Text="Ellipse2.Top"></TextBox>
    </StackPanel>
    </GroupBox>
    </uc:LeafControlItem.ItemContent>
    </uc:LeafControlItem>
    </uc:LeafControl.Items>
    </uc:LeafControl>
    </uc:LeafControlItem.ItemDetails>
    </uc:LeafControlItem>


  • 当我尝试访问基本自定义控件中的“项目”时。为什么添加所有子自定义控件?我应该怎么做才能让每个自定义控件对象(基本和子对象)都有单独的“项目”。

    我在基本自定义控件中使用了依赖属性,如下所示:
     #region LeafControlItemCollection 

    public List<LeafControlItem> Items
    {
    get { return (List<LeafControlItem>)GetValue(ItemsProperty); }
    set { SetValue(ItemsProperty, value); }
    }

    public static readonly DependencyProperty ItemsProperty =
    DependencyProperty.Register(
    "Items", typeof(List<LeafControlItem>), typeof(LeafControl),
    new FrameworkPropertyMetadata(new List<LeafControlItem>(), null, null)
    );
    #endregion

    请建议我哪里做错了。

    最佳答案

    问题在于依赖属性标识符的声明,即 ItemsProperty .您提供了一个 default value as new List<LeafControlItem>() .通过这样做,您创建了一个 singleton instance for your list在幕后。

    读出来 here ,它描述了您在默认初始化列表 DP 时遇到的完全相同的问题。从该链接引用 -

    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. Therefore you must be careful to not use the singular static collection defined by the collection property metadata as the working default value for newly created instances of your type. Instead, you must make sure that you deliberately set the collection value to a unique (instance) collection as part of your class constructor logic. Otherwise you will have created an unintentional singleton class.



    将默认值指定为 new List<LeafControlItem>() ,制作 LeafControl 的所有实例共享相同的列表实例。因此,该列表中对象的任何添加和删除都将反射(reflect)在 LeafControl 的所有实例中。所以,实际上您已经在此处为 LeafControl 的所有实例创建了单例列表。

    首先,您应该避免在默认值中指定新列表 -
    public static readonly DependencyProperty ItemsProperty =
    DependencyProperty.Register(
    "Items", typeof(List<LeafControlItem>), typeof(LeafControl));

    其次,您应该通过在 ListControl 类的构造函数中设置它来将其初始化为新列表,以便每个实例都有自己的列表份额 -
    public LeafControl()
    {
    SetValue(ItemsPropertyKey, new List<LeafControlItem>());
    }

    关于wpf - 自定义控件依赖属性集合,统计嵌套控件项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19755866/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com