C#WPF添加PropertyGridControl组合框

时间:2017-07-18 13:48:01

标签: c# wpf devexpress

我已经搜索了我的问题的答案,找到了一些答案,但他们没有帮我解决问题。

在这种情况下,单击我有PropertiesGridControl的列时有一个网格,其中包含一组存储在数据库中的参数。其中一个属性应该看起来像ComboboxEdit,其中包含数据库中的数据列表。但结果有空行。请帮帮我)

directoryVar = new Property("Directory", "Source", "", typeof(States));
propertyList.Add(directoryVar);
new DynamicPropertyGrid().FillGrid(PropertyGridControlByElements, propertyList);

[TypeConverter(typeof(List2PropertyConverter))]
public class States
{
}

public class List2PropertyConverter : StringConverter
{
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        //True - means show a Combobox
        //and False for show a Modal 
        return true;
    }

    public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
    {
        //False - a option to edit values 
        //and True - set values to state readonly
        return true;
    }

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        Proxies.ServicesClient services2 = new Proxies.ServicesClient();
        services2.ClientCredentials.UserName.UserName = Application.Current.Properties["UserID"].ToString();
        services2.ClientCredentials.UserName.Password = Application.Current.Properties["Password"].ToString();

        DataSet dataSet2 = services2.GetDictionariesList();
        List<DictionaryList> dictList = new List<DictionaryList>();
        DictionaryList DictId = null;
        DictionaryList DictName = null;

        foreach (DataRow dict in dataSet2.Tables[0].Rows)
        {
            string dictId = dict[0].ToString();
            string dictName = dict[1].ToString();
            dictList.Add(new DictionaryList(dictId, dictName));
        }
        services2.Close();
        return new StandardValuesCollection(dictList);
    }
}

namespace Controls
{
    [TypeConverter(typeof(Property))]
    public class Property
    {
        public string Name;
        [JsonIgnore]
        public string Category;
        public object iValue;
        public Type type;

        public Property(string name, string category, object value, Type type)
        {
            Name = name;
            Category = category;
            Value = value;
            this.type = type;
        }

        public Type Type
        {
            get { return type; }
        }

        public object Value
        {
            get
            {
                return iValue;
            }
            set
            {
                iValue = value;
            }
        }
    }

    public class DictionaryList
    {
        public string DictId;
        [JsonIgnore]
        public string DictName;

        public DictionaryList(string dictId, string dictName)
        {
            DictId = dictId;
            DictName = dictName;
        }
    }

    public class DynamicPropertyGrid
    {
        public void FillGrid(PropertyGridControl propertyGrid, List<Property> propertyList)
        {
            propertyGrid.DataContext = DataHelper.GetData(propertyList);
        }
    }

    public static class DataHelper
    {
        public static List<Property> PropertyList;

        public static MyDictionary GetData(List<Property> propertyList)
        {
            PropertyList = propertyList;

            MyDictionary dict = new MyDictionary();

            foreach (Property property in propertyList)
            {
                if (property.Name == "Directory")
                {
                    dict.Add(property.Name, property.Value);
                }
                else
                {
                    dict.Add(property.Name, property.Value);
                }
            }

            return dict;
        }
    }

    internal class List2PropertyConverter : StringConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            //True - means show a Combobox
            //and False for show a Modal 
            return true;
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            //False - a option to edit values 
            //and True - set values to state readonly
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            Proxies.ServicesClient services2 = new Proxies.ServicesClient();
            services2.ClientCredentials.UserName.UserName = Application.Current.Properties["UserID"].ToString();
            services2.ClientCredentials.UserName.Password = Application.Current.Properties["Password"].ToString();

            DataSet dataSet2 = services2.GetDictionariesList();
            List<DictionaryList> dictList = new List<DictionaryList>();
            DictionaryList DictId = null;
            DictionaryList DictName = null;

            foreach (DataRow dict in dataSet2.Tables[0].Rows)
            {
                string dictId = dict[0].ToString();
                string dictName = dict[1].ToString();
                dictList.Add(new DictionaryList(dictId, dictName));
            }
            services2.Close();
            return new StandardValuesCollection(dictList);
        }
    }

    public class MyDictionary : Dictionary<string, object>, ICustomTypeDescriptor
    {
        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        string ICustomTypeDescriptor.GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        string ICustomTypeDescriptor.GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        TypeConverter ICustomTypeDescriptor.GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        public bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            //True - means show a Combobox
            //and False for show a Modal 
            return true;
        }

        public bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            //False - a option to edit values 
            //and True - set values to state readonly
            return true;
        }

        object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            List<MyDictionaryPropertyDescriptor> properties = new List<MyDictionaryPropertyDescriptor>();

            foreach (string key in this.Keys)
            {
                properties.Add(new MyDictionaryPropertyDescriptor(key, this));
            }
            return new PropertyDescriptorCollection(properties.ToArray());
        }

        public PropertyDescriptorCollection GetProperties()
        {
            return TypeDescriptor.GetProperties(this, true);
        }

        object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }
    }

    public class MyDictionaryPropertyDescriptor : PropertyDescriptor
    {
        private MyDictionary parent;
        private string propertyName;

        public MyDictionaryPropertyDescriptor(string _propertyName, MyDictionary _parent)
            : base(_propertyName, null)
        {
            parent = _parent;
            propertyName = _propertyName;
        }

        public override bool CanResetValue(object component)
        {
            return true;
        }

        public override bool ShouldSerializeValue(object component)
        {
            return false;
        }

        public override object GetValue(object component)
        {
            return ((MyDictionary)component)[this.Name];
        }

        public override void ResetValue(object component)
        {
        }

        public bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            //True - means show a Combobox
            //and False for show a Modal 
            return true;
        }

        public bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            //False - a option to edit values 
            //and True - set values to state readonly
            return true;
        }

        public override void SetValue(object component, object value)
        {
            ((MyDictionary)component)[this.Name] = value;
        }

        public override Type ComponentType
        {
            get { return typeof(MyDictionary); }
        }

        public override Type PropertyType
        {
            get
            {
                return (parent[this.Name] == null) ? typeof(System.String) : parent[this.Name].GetType();
            }
        }
    }
}

0 个答案:

没有答案