简体   繁体   English

重建用户控件时,用户控件自定义属性会丢失状态

[英]User Control custom properties lose state when user control is rebuilt

I have a user control with custom properties as follows:我有一个带有自定义属性的用户控件,如下所示:

[DefaultValue(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Description("Gets or sets whether the \"Remove\" button is visible.")]
public bool ShowRemoveButton
{
    get
    {
        return this.removeButton.Visible;
    }
    set
    {
        this.removeButton.Visible = value;
    }
}

The control contains a standard button control.该控件包含一个标准按钮控件。 This property is used to show or hide the button.该属性用于显示或隐藏按钮。 The user control is built in a separate project assembly.用户控件内置于单独的项目程序集中。 I placed it on a form and I can set and unset the above property and everything appears to be working just fine.我把它放在一个表单上,我可以设置和取消设置上面的属性,一切似乎都很好。 However, when the project containing the user control is rebuilt, the property value flips to "false", which is not the default.但是,当重新生成包含用户控件的项目时,属性值会翻转为“false”,这不是默认值。

How can I prevent the custom property from losing/altering its state when the control is rebuilt?重建控件时,如何防止自定义属性丢失/更改其状态?

The problem is that the DefaultValueAttribute only tells the designer what the default value is for the property.问题是DefaultValueAttribute只告诉设计者属性的默认值是什么。 It controls whether the property is displayed in bold , and what the value gets reset to when you right-click on the property and choose "Reset" from the context menu.它控制属性是否以粗体显示,以及当您右键单击属性并从上下文菜单中选择“重置”时,该值将重置为什么。

What it doesn't do is set the property to a particular value at run-time.而那些没有做的是属性设置为在运行时一个特定的值。 To do that, you'll need to place code in your user control's constructor method.为此,您需要在用户控件的构造函数方法中放置代码。 For example:例如:

// set default visibility
this.removeButton.Visible = true;

Otherwise, as you've described, the value of the property will be reset when you rebuild the project.否则,正如您所描述的,当您重建项目时,该属性的值将被重置。 It will show up in bold in the Properties window of the designer, because it doesn't match the default (as specified in the DefaultValueAttribute ), but that attribute doesn't change what the value is set to.它将在设计器的“属性”窗口中以粗体显示,因为它与默认值(如DefaultValueAttribute指定的)不匹配,但该属性不会更改该值的设置。

An easy way to keep any contained controls' properties from being reset upon (de)serialization of related properties is to use a private backing field for the property and assign a default value that matches the DefaultValue attribute's parameter .防止任何包含的控件的属性在相关属性的(反)序列化时重置的一种简单方法是使用属性的私有支持字段并分配与DefaultValue属性的参数匹配的DefaultValue In this case, that's the _showRemoveButton = true declaration/assignment below.在这种情况下,就是下面的_showRemoveButton = true声明/赋值。

[DefaultValue(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Description("Gets or sets whether the \"Remove\" button is visible.")]
public bool ShowRemoveButton
{
    get
    {
        return _showRemoveButton;
    }
    set
    {
        _showRemoveButton = value;
        this.removeButton.Visible = value;
    }
}
private bool _showRemoveButton = true;

Old post but it guided me to the solution for my problem.旧帖子,但它引导我找到解决问题的方法。 I had excatly the same effect, that the state of my property was lost upon rebuild.我有完全相同的效果,我的财产在重建时失去了状态。 In my case the property is a class itself, that provides a bunch of Booleans.在我的情况下,该属性是一个类本身,它提供了一堆布尔值。

For me the solution was to change the bevhaviour how the designers serlializes the property.对我来说,解决方案是改变设计师序列化属性的行为。

   [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public SomePropertyClass SomeProperty
{
    get { return _SomeProperty; }
    set { _SomeProperty = Value; }
}

By using DesignerSerializationVisibility.Content you tell the designer to serialize the content of the property class (my Boolean contained in SomePropertyClass) rather than the property itself.通过使用 DesignerSerializationVisibility.Content,您可以告诉设计器序列化属性类的内容(我的布尔值包含在 SomePropertyClass 中)而不是属性本身。 I guess another approach would be to make SomePropertyClass serializable, but the above approach was quicker to implement.我想另一种方法是使 SomePropertyClass 可序列化,但上述方法实施起来更快。

Maybe that helps somebody.也许这对某人有帮助。 Sascha萨沙

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

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