Designer 不会为子控件的属性生成代码.为什么? [英] Designer does not generate code for a property of a subcontrol. Why?

查看:32
本文介绍了Designer 不会为子控件的属性生成代码.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了继承 UserControl 的 SubCtrl.它没有代码.然后我创建了 Ctrl,它也继承了 UserControl.它有一个 SubCtrl 并且它唯一的代码意味着公开它,所以它出现在 Ctrl 的属性列表中:

I created SubCtrl inheriting UserControl. It has no code. I created then Ctrl, which also inherits UserControl. It has a SubCtrl in it and its only code means to expose it publicly so it appears in the property list of Ctrl:

public subctrl.SubCtrl SUBCTRL
{
    get { return this.subCtrl1; }
}

然后我创建了一个简单的 Form 项目,其中只有一个 Ctrl 而没有代码.如我所愿,SUBCTRL 出现在 Ctrl 的属性列表中,因此我可以更改内容.我更改了背景颜色(例如,更改为红色),并且设计器中的 subctrl 变为红色.

Then I created a simple Form project which only has a Ctrl in it and no code. As I wanted, SUBCTRL appears in the property list of Ctrl so I can change things. I changed the background color (say, to red), and the subctrl turned red in the designer.

但神奇的是,当我运行该项目时,它又变回了标准的灰色.似乎在 Form1.Designer.cs 中没有生成代码来将 SUBCTRL 的背景颜色更改为红色.如果我手写它,它可以工作,但这不是我想要的.显然,它应该是自动的.

But magically, when I run the project, it turns back to the standard gray. It appears that no code is generated in Form1.Designer.cs to change SUBCTRL's back color to red. If I write it by hand, it works, but that's not what I want. It should be automatic, obviously.

另一方面,Ctrl 的行为正常.代码已生成,一切正常.

The Ctrl, on the other hand, behaves normally. The code is generated and everything works happily.

子控件有什么问题?

推荐答案

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 添加到属性.

这篇关于Designer 不会为子控件的属性生成代码.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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