天天看點

c#嵌套屬性在屬性控件propertyGrid中的應用

1、我們經常使用propertyGrid綁定一個含有衆多屬性成員的類的對象,進而實作對這個類中屬性參數值的設定和讀取,如下圖:

c#嵌套屬性在屬性控件propertyGrid中的應用

我們可以看出,這個類如下:

public   class MotionParameter
    {
        [DisplayName("起始速度"),  Description("闆卡的起始速度"), ReadOnly(false )]
        public   double StartVel { get; set; }
        [DisplayName("高速運作速度"),  Description("闆卡告訴運作時的速度"), ReadOnly(false )]
        public double Vel { get; set; }
        public double Acc { get; set; }
        public double Dec { get; set; }

        public double Dis { get; set; }
        public double time { get; set; }
        public Jerk jerk { get; set; } = Jerk.S;
    }
           

對于以上用法,在運動控制中隻能用于設定一個軸的參數,但是當一台機器有很多個軸的時候,比如四個軸,難道我們要使用四個propertyGrid控件嗎?答案是否定的,我們可以有更好的方法,利用嵌套屬性來實作,先看效果圖,如下:

c#嵌套屬性在屬性控件propertyGrid中的應用

也就是說我們建立一個類,這個類中的屬性成員的類就是之前提到的MotionParameter自定義類,而不是一直系統固有的類,代碼如下:

[Serializable]

public class DeltaMotionParameter

{

[DisplayName("軸1"), Category("軸參數設定"), Description("")]
    public MotionParameter motionParameter1
    {
        get
        {
            return Axis1Parm;
        }

        set
        {
           Axis1Parm=value ;
        }
    }
    [DisplayName("軸2"), Category("軸參數設定"), Description(""), ReadOnly(false)]

    public MotionParameter motionParameter2
    {
        get
        {
            return Axis2Parm;
        }

        set
        {
             Axis2Parm=value;
        }

    }
    [DisplayName("軸3"), Category("軸參數設定"), Description(""), ReadOnly(false)]
    public MotionParameter motionParameter3
    {
        get
        {
            return Axis3Parm;
        }

        set
        {
            Axis3Parm=value ;
        }

    }
    [DisplayName("軸4"), Category("軸參數設定"), Description(""), ReadOnly(false)]
    public MotionParameter motionParameter4
    {
        get
        {
            return Axis4Parm;
        }

        set
        {
             Axis4Parm=value ;
        }

    }

    private MotionParameter Axis1Parm = new MotionParameter();
    private MotionParameter Axis2Parm = new MotionParameter();
    private MotionParameter Axis3Parm = new MotionParameter();
    private MotionParameter Axis4Parm = new MotionParameter();
}
           

再結合序列化/反序列化最終可以實作參數的設定。

c#