开发者

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
        }
    }

}

}


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.


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; }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜