设计器不会为子控件的属性生成代码。为什么?
我创建了 SubCtrl 继承 UserControl。它没有代码。 然后我创建了 Ctrl,它也继承了 UserControl。它有一个 SubCtrl ,其唯一的代码意味着将其公开,因此它出现在 Ctrl 的属性列表中:
public subctrl.SubCtrl SUBCTRL
{
get { return this.subCtrl1; }
}
然后我创建了一个简单的 Form 项目,其中只有一个 Ctrl 而没有代码。 正如我想要的,SUBCTRL 出现在 Ctrl 的属性列表中,这样我就可以更改内容。我更改了背景颜色(例如,更改为红色),并且子控件在设计器中变为红色。
但神奇的是,当我运行该项目时,它又变回标准灰色。 Form1.Designer.cs 中似乎没有生成任何代码来将 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; }
}
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.
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.
The Ctrl, on the other hand, behaves normally. The code is generated and everything works happily.
What's wrong with the subcontrol?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
添加到该属性。Add
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
to the property.