简体   繁体   中英

How to change default values in inherited control yet allow to reset their values with instance's designer?

Consider following scenario:

  1. Create new custom control inherited from DataGridView named MyDataGridView .
  2. Open MyDataGridView in designer.
  3. Change AllowUserToAddRows property of MyDataGridView custom control to false .
  4. Use MyDataGridView instance in form.
  5. Change AllowUserToAddRows property of the MyDataGridView instance to true .

What I expect would happen: my instance should allow user to add rows. It doesn't. The moment I run build, the property of the instance is reset to the value set in MyDataGridView definition.

So I'm obviously doing it wrong. How to do it right?

I want MyDataGridView to have a different set of default values for properties than standard DataGridView , but yet those properties should remain editable in application's designer. What is the most straightforward mean to achieve this?

Example

Ok, here's one way of doing this, but it's terrible! I'm still looking for an easier way:

using System.ComponentModel;
using System.Windows.Forms;

namespace Test {
    public partial class MyDataGridView : DataGridView {

        bool _AllowUserToAddRows;
        [Category("Behavior")]
        [Description("Value indicating whether the option to add rows is displayed to the user")]
        [DefaultValue(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public new bool AllowUserToAddRows {
            get { return _AllowUserToAddRows; }
            set { base.AllowUserToAddRows = _AllowUserToAddRows = value; } }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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