user635409
user635409

Reputation:

do not work "set" in some of property in design time

why in desin time do not work set "dataGrid" Property in my code.but property font,with working correct. I used of this componet on the a Form.(i test it with design time debuging)

enter code here 
namespace Example
{
  public partial class Component1 : System.Windows.Forms.DataGridView
  {

    public Component1()
    {
    }

    private DataGridViewColumn _gridcolumn;

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [TypeConverter(typeof ( ExpandableObjectConverter))]
    public DataGridViewColumn gridcolumn
    {
        get
        {
            if (_gridcolumn == null)
                _gridcolumn = new DataGridViewColumn();
            return _gridcolumn;
        }

        set                                                 //Do not Work Set in designTime 
        {
            _gridcolumn = value;
        }
    }


    private System.Drawing.Font _MyFont;
    public System.Drawing.Font MyFont
    {
        get
        {
            if (_MyFont == null)
                _MyFont = new System.Drawing.Font("tahoma", 8);
            return _MyFont;
        }
        set
        {
            _MyFont = value;                                            //Work correctly in design time
        }
    }

    int _with;
    public int withcustom
    {
        get
        {
            return _with;
        }
        set
        {
            _with = value;                                           //Work correctly in design time
        }
    }

}

}

Upvotes: 0

Views: 962

Answers (2)

TotPeRo
TotPeRo

Reputation: 6791

This is good example:

public partial class SCon : UserControl
    {
        public SCon()
        {
            InitializeComponent();
            if (Persoanas == null)
            {
                Persoanas = new List<Persoana>();
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public List<Persoan> Persoanas { get; set; }

    }

    [Serializable]
    public class Persoan   
    {
        public int Id { get; set; }
        public String Name { get; set; }
    }

Upvotes: 0

Adam Robinson
Adam Robinson

Reputation: 185663

Short Version

Either remove DesignerSerializationVisibility(DesignerSerializationVisibility.Content) from your property, or change it to DesignerSerializationVisibility(DesignerSerializationVisibility.Visible).

Long Version

Applying DesignerSerializationVisibility(DesignerSerializationVisibility.Content) to your property means that the WinForms designer will not generate code that sets the value of the property, but rather code that sets the value of the properties on the value of that property.

For example, say I have these two types:

public class MyControl : Control
{
    public class MyControlProperties
    { 
        public string Prop1 { get; set; }
        public int Prop2 { get; set; }
    }

    private MyControlProperties props;

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public MyControlProperties Properties
    {
        get
        {
            if(props == null) props = new MyControlProperties();

            return props;
        }
    }
}

When the designer generates the code for MyControl on a form, it will look something like this:

myControl.Properties.Prop1 = "foo";
myControl.Properties.Prop2 = 10;

So, instead of setting the value of the Properties property (which, in this code, is read-only; though it doesn't have to be, properties like this usually are), it's setting the values of the properties on the value of that property.

Upvotes: 4

Related Questions