简体   繁体   English

自定义UserControl-设计时支持控件重画

[英]Custom UserControl - Design-Time support for control repainting

I have created my custom UserControl with some custom properties for it. 我已经为其自定义UserControl创建了一些自定义属性。 For example: 例如:

[Description("Example Description"),Category("CustomSettings"),DefaultValue("Transmedicom")]
public string DatabaseAddress
{
    get; set;
}

Everything works fine. 一切正常。 I can change custom property in code and in design-time . 我可以在代码和design-time更改custom property

What I'm looking for (and cannot find anything) now is: How could I repaint (reacreate) my UserControl in design-time when my custom property change in design-time. 我现在正在寻找(但找不到任何东西)的是:当我的自定义属性在设计时更改时,如何在设计时重新绘制(重新创建) UserControl

Let's say when DatabaseName will be changed to localhost UserControl will add and display some Label on my UserControl. 假设当DatabaseName更改为localhost UserControl将在UserControl添加并显示一些Label。 It's important to work in Design-Time . Design-Time工作很重要。

Nothing special there. 没什么特别的。 You just have to set the text to Label inside the property setter. 您只需要在属性设置器中将文本设置为Label That should update the UI. 那应该更新UI。

private string databaseAddress;
[Description("Example Description"), Category("CustomSettings"), DefaultValue("Transmedicom")]
public string DatabaseAddress
{
    get { return databaseAddress; }
    set 
    { 
        databaseAddress = value;
        yourLabel.Text = value;//Set value to Label or whatever
    }
}

Try this 尝试这个

private string _databaseAddress = "localHost";
[Description("Example Description"), Category("CustomSettings"), DefaultValue("Transmedicom")]
public string DatabaseAddress
{
    get
    {
        return _databaseAddress;
    } 
    set 
    {
        if(!string.IsNullOrEmpty(value))
        {
            _databaseAddress = value;
            lblAddress.Text = value;
            lblAddress.Invalidate();
        }
    }
}

Both previous answers are correct. 先前的两个答案都是正确的。
I just want to add that the user control can even react on resizing during design time, using the Layout event. 我只想补充一点,即用户控件甚至可以在设计时使用Layout事件对大小调整做出反应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM