在设计师:属性设置默认值,但在设置默认的属性不叫? [英] In Designer: Property default value is set, but property isn't called while setting default?

查看:255
本文介绍了在设计师:属性设置默认值,但在设置默认的属性不叫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一块code那是这样的:

I have a piece of code that goes something like this:

[DefaultValue(false)]
public bool Property
{
    set
    {
        blah = value;
        someControl.Visible = value;
    }
    get
    {
        return blah;
    }
}

当我看到在设计师的属性,该属性设置为false(或true,如果我发挥它)。结果
但属性不实际设置。在可见控件的值不会改变。

When I look at the properties in the designer, the property is set to false (or true if I play with it).
But the property isn't actually set. The Visible value of the control isn't changed.

我怎样才能使设计人员实际设置的默认值的属性?

How can I make the designer actually set the property with the default value ?

推荐答案

在[默认值]属性只是一个提示设计师和串行。它完全是由你来确保你在属性承诺的默认值实际上是在财产的价值。属性setter,因为串行检测到属性的当前值等于默认值,因此可以的省略的属性赋值不会被调用你的情况。这是一种优化,它使的InitializeComponent()更小,更快。

The [DefaultValue] attribute is only a hint to the designer and the serializer. It is completely up to you to ensure that the default value you promised in the attribute is in fact the value of the property. The property setter doesn't get called in your case because the serializer detected that the current value of the property equals the default value and can thus omit the property assignment. It is an optimization, it makes InitializeComponent() smaller and faster.

您只需在构造函数初始化属性值确保这一点。要注意的是一个控件的Visible属性默认为true。

You ensure this simply by initializing the property value in your constructor. Beware that a control's Visible property defaults to true.

这篇关于在设计师:属性设置默认值,但在设置默认的属性不叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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