用户控件设计时属性未显示在属性窗口中
我有一个带有自定义属性的USERCONTROL:
public Size TestSize { get; set; }
我希望此属性在USERCONTROL的设计时间内出现在属性窗口中,而不仅仅是将UserControl用作实例的地方(例如,在表单上拖动/删除时)。
例子: 在UserControl的设计师中,一个人可以设置诸如“ AllowDrop”,“ AutoScroll”,“ BackColor”等属性。我希望我的自定义属性在设计师中以相同的方式显示。
我已经考虑并尝试将属性添加到类似的属性中:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
但这似乎不是要走的路。
任何帮助都将受到赞赏!
I have a UserControl with a custom property:
public Size TestSize { get; set; }
I would like this property to show up in the properties window during design time of the UserControl, and not only where the UserControl is used as an instance (e.g. when dragged/dropped on a form).
Example:
In the designer of the UserControl one can set properties like "AllowDrop", "AutoScroll", "BackColor" etc. I would like my custom property to show up just the same way in the designer.
I have considered and tried adding attributes to the property like:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
but that does not seem to be the way to go.
Any help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您确实想在
UserControl1
的设计器中显示该属性,那么该属性应该属于其基类。这是因为设计器的工作方式在大多数情况下,您不需要在设计器中显示这些属性,除非您想从该基础派生多个控件并在派生控件的设计时配置该属性,但我假设您知道您的要求,现在您可以根据您的要求做出决定。
这就是设计器的工作方式:
.cs
文件(并查找InitializeComponent 方法或designer.cs 文件,还考虑与设计器相关的属性)。当然,还有很多其他事情发生,例如使扩展提供程序正常工作,或者在设计时预过滤或后过滤属性,但是在一般来说,它的工作原理就像我上面解释的那样。
更多信息/相关帖子
其他一些相关答案:
If you really want to show the property in the designer of
UserControl1
, then the property should belong to its base class. That's because of how designer worksIn most cases you don't need to show those properties in designer, unless you want to derive multiple controls from that base and configure that property in design-time of the derived control, but I assume you know your requirement and now you are able to make a decision base on your requiremets.
This is the way that designer works:
.cs
file (and looks for InitializeComponent method, or designer.cs file, also considers the designer-related attributes).Of course there are a lot of other things happening, like making the extender providers working, or pre-filtering or post-filtering properties in design-time, but in general it works like what I explained above.
More information / Related Posts
Some other related answers:
我知道旧帖子,但遇到了同样的问题。
重新编译并重新打开表单的设计器视图解决了我的问题。
在框架 4.5 中使用 VS2022 和 C#
Old post I know but just had the same problem.
Re-compiling and re-opening the designer view of the form solved the problem for me.
Using VS2022 and C# with framework 4.5