C#开发学习笔记:使用PropertyGridControl编写自定义属性

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DemoCollection
{
    public partial class FormPropertyGridControl : Form
    {
        public FormPropertyGridControl()
        {
            InitializeComponent();
        }

        private void FormPropertyGridControl_Load(object sender, EventArgs e)
        {
            CustomAttribute customAttribute = new CustomAttribute();
            CustomAttribute1 customAttribute1 = new CustomAttribute1();
            CustomAttribute2 customAttribute2 = new CustomAttribute2();

            //实例化一个CustomAttribute1类型的对象,将它赋值给customAttribute的CA1属性
            //这样CA1属性就会展示CustomAttribute1类型对象的属性成员
            customAttribute.CA1 = customAttribute1;
            customAttribute1.CustomDisplayFormart = customAttribute2;

            propertyGridControl1.SelectedObject = customAttribute;//设置要展示属性的对象
            propertyGridControl1.ExpandAllRows();//展开所有属性(包括子属性)
            propertyGridControl1.RowHeaderWidth = 100;//设置自定义属性左边属性名称的宽度

            //设置属性不按A-Z排序
            propertyGridControl1.OptionsBehavior.PropertySort = DevExpress.XtraVerticalGrid.PropertySort.NoSort;

            //设置属性按A-Z排序
            //propertyGridControl1.OptionsBehavior.PropertySort = DevExpress.XtraVerticalGrid.PropertySort.Alphabetical;

            DevExpress.XtraVerticalGrid.Rows.BaseRow br= propertyGridControl1.GetRowByCaption("自定义属性");

            //通过循环遍历设置属性的中文名称
            foreach (DevExpress.XtraVerticalGrid.Rows.PGridEditorRow per in br.ChildRows)
            {
                if (per.ChildRows.Count>0)
                {
                    //利用递归解决多层可扩展属性的caption的赋值
                    SetCustomAttributeCaption(per);
                }
                string dicKey = per.Properties.FieldName;
                if (CustomAttribute.dic.ContainsKey(dicKey))
                    per.Properties.Caption = CustomAttribute.dic[dicKey];
                per.Height = 23;//设置属性行高度
                
            }

            propertyGrid1.SelectedObject = customAttribute;//设置要展示属性的对象
            propertyGrid1.ExpandAllGridItems();//展开所有属性(包括子属性)

            

        }
        /// 
        /// 设置自定义属性的描述
        /// 
        private void SetCustomAttributeCaption(DevExpress.XtraVerticalGrid.Rows.PGridEditorRow PGridEditorRow)
        {
            foreach (DevExpress.XtraVerticalGrid.Rows.PGridEditorRow per_child in PGridEditorRow.ChildRows)
            {
                if (per_child.ChildRows.Count > 0)
                {
                    //利用递归解决多层可扩展属性的caption的赋值
                    SetCustomAttributeCaption(per_child);
                }
                //FieldName属性包含了该属性的父属性FieldName;通过 . 分割
                string[] per_child_FieldName= per_child.Properties.FieldName.Split('.');
                string dicKey = per_child_FieldName[per_child_FieldName.GetLength(0) - 1];
                if (CustomAttribute.dic.ContainsKey(dicKey))
                    per_child.Properties.Caption = CustomAttribute.dic[dicKey];
                per_child.Height = 23;//设置属性行高度
            }
        }
    }
    
    /// 
    /// 自定义属性类
    /// 
    class CustomAttribute
    {
        /// 
        /// 用于存放属性英文-中文的键值对
        /// 
        public static Dictionary dic = new Dictionary();
        public enum VisibleStatus
        {
            True,
            False
        }

        CustomAttribute1 _ca1;
        private string _CustomName = "";
        private string _CustomNameDesc = "";
        private VisibleStatus _Visible = VisibleStatus.False;
        private Color _CustomColor;
        private DateTime _CustomDateTime;
        private string _CustomDisplayFormart = "";
        private string _CustomFormartString = "";
        public CustomAttribute()
        {
            dic.Add("CustomName", "名称");
            dic.Add("CustomNameDesc", "名称描述");
            dic.Add("CustomVisible", "是否隐藏");
            dic.Add("CustomColor", "颜色");
            dic.Add("CustomDateTime", "时间");
            dic.Add("CA1", "可展开属性"); 
        }
       
        /// 
        /// 名称
        /// 
        [Browsable(true),Category("自定义属性")]        
        public string CustomName
        {
            get { return _CustomName; }
            set 
            { 
                _CustomName = value;
                
            }
        }

        /// 
        /// 名称描述
        /// 
        [Browsable(true), Category("自定义属性")]
        public string CustomNameDesc
        {
            get { return _CustomNameDesc; }
            set 
            { 
                _CustomNameDesc = value; 
                
            }
        }
        /// 
        /// 是否隐藏
        /// 
        [Browsable(true), Category("自定义属性")]
        public VisibleStatus CustomVisible
        {
            get { return _Visible; }
            set 
            { 
                _Visible = value; 
                
            }
        }

        /// 
        /// 颜色
        /// 
        [Browsable(true), Category("自定义属性")]
        public Color CustomColor
        {
            get { return _CustomColor; }
            set 
            { 
                _CustomColor = value;
                
            }
        }
        /// 
        /// 时间
        /// 
        [Browsable(true), Category("自定义属性")]
        public DateTime CustomDateTime
        {
            get { return _CustomDateTime; }
            set 
            { 
                _CustomDateTime = value;
                
            }
        }

        /// 
        /// 可展开属性
        /// 
        /// TypeConverter(typeof(ExpandableObjectConverter)):将CustomAttribute1类型的对象转为可扩展对象
        [Browsable(true), Category("自定义属性"),TypeConverter(typeof(ExpandableObjectConverter))]
        public CustomAttribute1 CA1
        {
            get { return _ca1; }
            set 
            { 
                _ca1 = value;
                
            }
        }
    }

    /// 
    /// 自定义属性类1
    /// 
    class CustomAttribute1
    {
        public CustomAttribute1()
        {
            CustomAttribute.dic.Add("CustomDisplayFormart", "显示类型");
            CustomAttribute.dic.Add("CustomFormartString", "类型格式"); 
        }
        private CustomAttribute2 _CustomDisplayFormart;
        private string _CustomFormartString = "";
        /// 
        /// 显示类型
        /// 
        [Browsable(true), TypeConverter(typeof(ExpandableObjectConverter))]
        public CustomAttribute2 CustomDisplayFormart
        {
            get { return _CustomDisplayFormart; }
            set 
            {
                _CustomDisplayFormart = value;
                
            }
        }
        /// 
        /// 类型格式
        /// 
        public string CustomFormartString
        {
            get { return _CustomFormartString; }
            set 
            { 
                _CustomFormartString = value;
                
            }
        }
    }
    /// 
    /// 自定义属性类2
    /// 
    class CustomAttribute2
    {
        public CustomAttribute2()
        {
            CustomAttribute.dic.Add("CustomDisplayFormartValue", "显示格式值");
            CustomAttribute.dic.Add("AllowUseCustomDisplayFormart", "是否启用显示格式");
        }
        private string _CustomDisplayFormartValue = "";
        private CustomAttribute.VisibleStatus _AllowUseCustomDisplayFormart = CustomAttribute.VisibleStatus.False;
        /// 
        /// 值
        /// 
        public string CustomDisplayFormartValue
        {
            get { return _CustomDisplayFormartValue; }
            set
            {
                _CustomDisplayFormartValue = value;

            }
        }
        /// 
        /// 是否启用
        /// 
        public CustomAttribute.VisibleStatus AllowUseCustomDisplayFormart
        {
            get { return _AllowUseCustomDisplayFormart; }
            set
            {
                _AllowUseCustomDisplayFormart = value;

            }
        }
    }
}

 

你可能感兴趣的:(C#开发学习笔记)