用户控件设计时属性未显示在属性窗口中

发布于 2025-01-18 20:47:53 字数 445 浏览 0 评论 0原文

我有一个带有自定义属性的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听,心雨的声音 2025-01-25 20:47:53

如果您确实想在UserControl1的设计器中显示该属性,那么该属性应该属于其基类。这是因为设计器的工作方式

  • 假设设计器的根元素是 UserControl1,那么您在属性窗口中看到的属性是 UserControl1 的基类(可能是 UserControl)的可浏览属性。要了解更多信息,请阅读答案的第二部分。

在大多数情况下,您不需要在设计器中显示这些属性,除非您想从该基础派生多个控件并在派生控件的设计时配置该属性,但我假设您知道您的要求,现在您可以根据您的要求做出决定。


这就是设计器的工作方式:

  1. 它反序列化.cs 文件(并查找InitializeComponent 方法或designer.cs 文件,还考虑与设计器相关的属性)。
  2. 它创建基类的实例并将其添加到设计图面,并应用反序列化的属性值。 (因此,您在设计器中看到的实例是基类的实例。这是一个技巧。例如,这就是为什么如果您有一个抽象基类,则如果没有一些解决方法,就不能将派生类作为根设计器。
  3. )基于反序列化代码创建整个控制/组件树。 (因此设计表面上的控件是控件的真实实例)

当然,还有很多其他事情发生,例如使扩展提供程序正常工作,或者在设计时预过滤或后过滤属性,但是在一般来说,它的工作原理就像我上面解释的那样。

更多信息/相关帖子

其他一些相关答案:

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 works

  • Assuming the root element of the designer is UserControl1, then the properties that you see in properties window, are browsable properties of UserControl1's base class (which is probably a UserControl). To learn more about it, read the second section of the answer.

In 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:

  1. It deserialize the .cs file (and looks for InitializeComponent method, or designer.cs file, also considers the designer-related attributes).
  2. It creates an instance of the base class and add it to the design surface, and applies the deserialized property values. (So the instance that you see in designer, is an instance of the base class. It's a trick. For example, that's why if you have an abstract base class, you cannot have the derived class as root designer without some workarounds.)
  3. It creates the whole control/component trees based on the deserialized code. (So the controls which are on design surface are real instances of the controls)

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:

数理化全能战士 2025-01-25 20:47:53

我知道旧帖子,但遇到了同样的问题。
重新编译并重新打开表单的设计器视图解决了我的问题。
在框架 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文