简体   繁体   English

C#中用户控件的属性总是false?

[英]The property of user control always be false in C#?

I create a custom User Control which inherits from DataGridViewColumn.我创建了一个从 DataGridViewColumn 继承的自定义用户控件。

Moreover, I added some properties in the control, but I can't modify them in design-time.而且,我在控件中添加了一些属性,但我无法在设计时修改它们。

Even I set the default value as true, it's still false.即使我将默认值设置为true,它仍然是false。

code:代码:

public class ParaGridViewColumn : DataGridViewColumn
{                
    private bool _Necessary;
    private bool _ReadOnlyEmpty;

    [Description("Default cell status."), Category("Behavior"), DefaultValue(false)]
    public bool Necessary { get { return _Necessary; } set { _Necessary = value;} }                        
    [Description("When ReadOnly is true, clear value on the cell or not."), Category("Behavior"), DefaultValue(true)]
    public bool ReadOnlyEmpty { get { return _ReadOnlyEmpty; } set { _ReadOnlyEmpty = value; }}        
  
    public ParaGridViewColumn()
    {
        this.CellTemplate = new ParaGridViewCell();
    }
}

The new properties can shown on the window, but their default value are false.新属性可以显示在窗口上,但它们的默认值为 false。

I change them to true, entering and opening this window again, it's still false.我把它们改成真,再次进入并打开这个窗口,它仍然是假的。

However, I can modify other properties regularly.但是,我可以定期修改其他属性。 It means I didn't lock this control.这意味着我没有锁定这个控件。 在此处输入图片说明

Why is that?这是为什么? Did I make something wrong?我做错了什么吗?

Thanks a lot.非常感谢。

The first problem is with [DefaultValue(true)] for ReadOnlyEmpty while not setting the true value for it.第一个问题是ReadOnlyEmpty [DefaultValue(true)]而没有为其设置真实值。

You have not set the default value for property to true.您尚未将属性的默认值设置为 true。 In fact [DefaultValue(true)] help to CodeDomeSerizalizer to serialize value for this property if the property value is not equals to this default value.事实上,如果属性值CodeDomeSerizalizer于此默认值, [DefaultValue(true)]帮助CodeDomeSerizalizer序列化此属性的值。 You should set ReadOnlyEmpty= true in constructor or _ReadOnlyEmpty instead.您应该在构造函数或 _ReadOnlyEmpty 中设置ReadOnlyEmpty= true

private bool _ReadOnlyEmpty=true;

The problem here is when you set property to true in property grid, when closing the form, serializer will not serialize true value, because you said it is default but you have not set it to true, so it will remain false.这里的问题是,当你在property grid中将property设置为true,关闭表单时,serializer不会序列化true值,因为你说它是默认值但你没有设置它为true,所以它会保持false。

The second problem is that if you want values persist, you should override Clone of base class and provide a clone copy that contains your custom properties.第二个问题是,如果你想坚持的值,你应该重写Clone基类,并提供包含您的自定义属性的克隆副本。

public override object Clone()
{
    ParaGridViewColumn column = (ParaGridViewColumn)base.Clone();
    //Uncomment if you have ParaGridViewCell
    //column.CellTemplate = (ParaGridViewCell)CellTemplate.Clone();
    column.Necessary  = this.Necessary;
    column.ReadOnlyEmpty = this.ReadOnlyEmpty;
    return column;
}

Well, I found the solution, losing an override clone function.好吧,我找到了解决方案,失去了覆盖克隆功能。

public override object Clone()
{
    ParaGridViewColumn c = (ParaGridViewColumn)base.Clone();

    c._Necessary = this._Necessary;      
    c._ReadOnlyEmpty = this._ReadOnlyEmpty;            
    return c;
} 

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

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