在设计时设置自定义控件的内部控件的属性 [英] Set properties of inner Control of Custom Control in design time

查看:106
本文介绍了在设计时设置自定义控件的内部控件的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间我从网络回到Windows开发,感觉缺少某些东西...

好吧,问题是我在自定义控件中具有Label属性.现在,当我在设计时设置其Text属性(标签)时,它看起来不错,但是在运行时,Text将恢复为默认设置.这是因为design.cs文件没有这样的代码

Its a long time I came back to windows development from web and feeling something is missing...

Well, the question is I have Label property in a custom control. Now when I set its Text property(Label''s) at design time, it appears good but when run, Text comes back to default. Its because the design.cs file do not have code like this

this.CustomControl1.Title.Text = "something to appear";




源代码中的以下代码片段





The following code snippet from source


Label _title;
public Label Title
{
    get { return _title; }
  // set { _title = value; }
}



并在构造函数(.ctor)上



and on constructor (.ctor)

 _title = new Label();
            _title.AutoSize = true;
            _title.Text = "Title";
....

推荐答案

最后,我抓住了它的脖子

我只需要在属性上方放置[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

最终外观

Finally, I grabbed its neck

all I needed was to put [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] above the property

final look

Label _title;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Label Title
{
    get { return _title; }
    // set { _title = value; }
}


如果未定义设置器,我认为您不能设置其属性.它会为您解决编译错误.

如果控件是由您创建的,请确保设置器正确设置了该值.您可能就是这种情况.




Xmen W.K. :即使有二传手也行不通.在这种情况下,Plus setter仅适用于Label.标签属性可以更改,即使只有吸气剂也可以更改.
If there is no setter defined, I dont think you can set its properties. It will through compilation error for you.

If the control is created by you, make sure that the setter sets the value properly. This might be the case for you.




Xmen W.K. : Even there is setter it doesn''t work. Plus setter in that case will be only for Label. Label properties can be change even there is only getter.


似乎您偶然发现了IDE错误.如果您使用标题"的子属性,则Designer不会自动添加代码.您需要手动执行此操作或执行OnLoad事件.
It seems you have stumbled upon IDE bug. Designer does not automatcly add code if you use sub-propeties of Title. You would need to do it manualy or do it OnLoad event.


这篇关于在设计时设置自定义控件的内部控件的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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