类的集合属性 [英] Collection property of a class

查看:95
本文介绍了类的集合属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来像是一个愚蠢的问题,但它已经让我发疯了一段时间.

我有一个用户控件,其属性为"Items",类型为List< subitem>
子项仅由字符串和图标图像组成.

每当我在设计时自定义它就可以了.但是,当我运行该应用程序时,它将属性重置为默认的空集合.我假设我必须在属性上设置适当的属性,以允许编译器对其进行序列化……但是到目前为止,我还没有运气.帮我pleeeeeease吗?!

摘录:

This feels like a stupid question, but it''s been driving me insane for a while now.

I have a user control with a property ''Items'' that is of type List<subitem>
sub item just consists of a string and an icon image.

Whenever I customize it at design-time it''s fine. But when I run the application, it resets the property back to it''s default empty collection. I assume I have to set the appropriate attribute on the property to allow the compiler to serialize it or something... But so far I''ve had no luck. Help me pleeeeeease?!

Extract:

public class ListDisplay : UserControl
{
  private SubItemCollection mSubItems = new SubItemCollection();
  public SubItemCollection SubItems
  {
    get { return mSubItems; }
    set { mSubItems = value; }
  }
}

public class SubItemCollection : List<subitem> { ... }
public class SubItem { ... }
</subitem>

推荐答案

您需要提供使用Usercontrol的代码.我怀疑问题出在使用代码中.

可能是您正在使用UserControl 的实例设置属性,并尝试在另一个实例中获取该属性,而不是包含所设置的属性值的实例.尝试调试并查找此类情况.

提供用户控件的常用代码将有助于更好地诊断问题.

最好的祝福.
You need to provide the codes that are using the Usercontrol. I suspect, problem is in the consuming codes.

May be you are setting the property with an instance of the UserControl and trying to get the property in another instance, instead of the instance that contains the property value being set. Try to debug and look for such situations.

Providing consuming codes of the user control will help better-diagnosing the problem.

Best wishes.


这篇关于类的集合属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆