Mehrere Dateninstanzen im Propertygrid anzeigen



  • Hallo.

    Ich habe folgende Klasse definiert:

    public class CClass
        {
            public string MyTestString = "Static string";
    
            public MyPropertyGridItemCollection DynamicData= new MyPropertyGridItemCollection();
            public List<MyPropertyGridItem> PGItemStack = new List<MyPropertyGridItem>();
            public List<double> PGValues = new List<double>();
    
            [Description("Static test string.")]
            [Category("Static elements")]
            [ParenthesizePropertyName(true)]
            public string MyTestStringAlias
            {
                get { return MyTestString; }
                set
                {
                    MyTestString = value;
                }
            }
    
            public CClass(){
    
                for (int i = 0; i < 5; i++)
                {
                    PGValues.Add(0.0);
                    PGItemStack.Add(new MyPropertyGridItem { DisplayName = "Property " + i, PropertyTypeName = "System.Double", Description = i.ToString() + "ter double Wert", Category = "First Category", Value = PGValues[i], ReadOnly = false});
                }
    
                foreach(MyPropertyGridItem MPI in PGItemStack)
                {
                    DynamicData.Add(MPI.DisplayName, MPI);
                }
            }
        }
    

    Wenn ich jetzt PropertyGrid.SelectedItems = Instanz_von_CClass mache, wird nur die Variable MyTestString im Propertygrid angezeigt, was ja auch logisch ist. Wenn ich PropertyGrid.SelectedItems = DynamicData mache, werden alle dynamischen Werte angezeigt - die Sache mit dem PropertyGridItemCollection funktioniert also.
    Ich würde jetzt gerne alle Werte im PropertyGrid anzeigen lassen, da ich es dynamisch steuern möchte. Könnte ihr mir sagen, wie ich das bewerkstelligen könnte? SelectedItems (Betonung liegt auf der Mehrzahl) zeigt soviel ich weiß nur die gemeinsame Schnittmenge mehrerer Objekte an.

    Vielen Dank,
    CJens

    Wenn auch nicht notwendig: Anbei der Code zu den zusätzlichen Klassen, welche in dem Code oben instanziert werden:

    public class MyPropertyGridItem
        {
            private bool _bFirstAssignment = true;
            private object _objValue;
            private object _objResetValue;
    
            public string DisplayName { get; set; }
    
            public string Name { get; internal set; }
    
            public string Category { get; set; }
    
            public string Description { get; set; }
    
            public string PropertyTypeName { get; set; }
    
            public object Value
            {
                get
                {
                    return _objValue;
                }
                set
                {
                    _objValue = value;
                    ResetValue = value;
                }
            }
    
            public object ResetValue
            {
                get
                {
                    return _objResetValue;
                }
                set
                {
                    if (_bFirstAssignment)
                    {
                        _bFirstAssignment = false;
                        _objResetValue = value;
                    }
                }
            }
    
            public object DefaultValue { get; set; }
    
            public bool ReadOnly { get; set; }
    
            public bool Browsable { get; set; }
    
            public string EditorTypeName { get; set; }
    
            public string ConverterTypeName { get; set; }
    
            public object Tag { get; set; }
        }
    
    public class MyPropertyGridItemCollection : IDictionary<string, MyPropertyGridItem>, ICustomTypeDescriptor
        {
            private Dictionary<string, MyPropertyGridItem> _cobData = new Dictionary<string, MyPropertyGridItem>();
    
            #region IDictionary<string,MyPropertyDataItem> Members
    
            public bool ContainsKey(string strKey)
            {
                return _cobData.ContainsKey(strKey);
            }
    
            public ICollection<string> Keys
            {
                get
                {
                    return _cobData.Keys;
                }
            }
    
            bool IDictionary<string, MyPropertyGridItem>.Remove(string strKey)
            {
                return _cobData.Remove(strKey);
            }
    
            public bool TryGetValue(string strKey, out MyPropertyGridItem objValue)
            {
                return _cobData.TryGetValue(strKey, out objValue);
            }
    
            public ICollection<MyPropertyGridItem> Values
            {
                get
                {
                    return _cobData.Values;
                }
            }
    
            public MyPropertyGridItem this[string strKey]
            {
                get
                {
                    return _cobData[strKey];
                }
                set
                {
                    _cobData[strKey] = value;
                }
            }
    
            public void Add(string strKey, MyPropertyGridItem objValue)
            {
                objValue.Name = strKey;
                _cobData.Add(strKey, objValue);
            }
            #endregion
    
            #region ICollection<KeyValuePair<string,MyPropertyDataItem>> Members
    
            public void Add(KeyValuePair<string, MyPropertyGridItem> objItem)
            {
                objItem.Value.Name = objItem.Key;
                _cobData.Add(objItem.Key, objItem.Value);
            }
    
            public void Clear()
            {
                _cobData.Clear();
            }
    
            public bool Contains(KeyValuePair<string, MyPropertyGridItem> objItem)
            {
                return _cobData.Contains(objItem);
            }
    
            public void CopyTo(KeyValuePair<string, MyPropertyGridItem>[] aobjData, int iArrayIndex)
            {
                throw new NotImplementedException();
            }
    
            public int Count
            {
                get
                {
                    return _cobData.Count;
                }
            }
    
            public bool IsReadOnly
            {
                get
                {
                    return false;
                }
            }
    
            public bool Remove(KeyValuePair<string, MyPropertyGridItem> objItem)
            {
                return _cobData.Remove(objItem.Key);
            }
    
            #endregion
    
            #region IEnumerable<KeyValuePair<string,MyPropertyDataItem>> Members
    
            public IEnumerator<KeyValuePair<string, MyPropertyGridItem>> GetEnumerator()
            {
                foreach (KeyValuePair<string, MyPropertyGridItem> objItem in _cobData)
                {
                    yield return objItem;
                }
            }
            #endregion
    
            #region IEnumerable Members
            IEnumerator IEnumerable.GetEnumerator()
            {
                return this.GetEnumerator();
            }
            #endregion
    
            #region ICustomTypeDescriptor Members
    
            public string GetClassName()
            {
                return TypeDescriptor.GetClassName(this, true);
            }
    
            public AttributeCollection GetAttributes()
            {
                return TypeDescriptor.GetAttributes(this, true);
            }
    
            public string GetComponentName()
            {
                return TypeDescriptor.GetComponentName(this, true);
            }
    
            public TypeConverter GetConverter()
            {
                return TypeDescriptor.GetConverter(this, true);
            }
    
            public EventDescriptor GetDefaultEvent()
            {
                return TypeDescriptor.GetDefaultEvent(this, true);
            }
    
            public PropertyDescriptor GetDefaultProperty()
            {
                return TypeDescriptor.GetDefaultProperty(this, true);
            }
    
            public object GetEditor(Type editorBaseType)
            {
                return TypeDescriptor.GetEditor(this, editorBaseType, true);
            }
    
            public EventDescriptorCollection GetEvents(Attribute[] aobjAttributes)
            {
                return TypeDescriptor.GetEvents(this, aobjAttributes, true);
            }
    
            public EventDescriptorCollection GetEvents()
            {
                return TypeDescriptor.GetEvents(this, true);
            }
    
            public object GetPropertyOwner(PropertyDescriptor objPropertyDescriptor)
            {
                return this;
            }
    
            public PropertyDescriptorCollection GetProperties(Attribute[] aobjAttributes)
            {
                return GetProperties();
            }
    
            public PropertyDescriptorCollection GetProperties()
            {
                MyPropertyGridItemDescriptor objDataPropertyDescriptor;
                PropertyDescriptorCollection cobjPropertyDescriptorCollection = new PropertyDescriptorCollection(null);
    
                foreach (KeyValuePair<string, MyPropertyGridItem> objItem in _cobData)
                {
                    objDataPropertyDescriptor = new MyPropertyGridItemDescriptor(this, objItem.Key);
                    cobjPropertyDescriptorCollection.Add(objDataPropertyDescriptor);
                }
                return cobjPropertyDescriptorCollection;
            }
            #endregion
        }
    
    public class MyPropertyGridItemDescriptor : PropertyDescriptor
        {
            private MyPropertyGridItemCollection _objPropertyCollection = null;
            private string _strKey;
            private bool _bResetDefaultSwitch = true;
    
            public MyPropertyGridItemDescriptor(MyPropertyGridItemCollection objPropertyCollection, string strKey)
                : base(strKey, null)
            {
                this._objPropertyCollection = objPropertyCollection;
                this._strKey = strKey;
            }
    
            public override AttributeCollection Attributes
            {
                get
                {
                    return new AttributeCollection(null);
                }
            }
    
            public override bool CanResetValue(object component)
            {
                return true;
            }
    
            public override Type ComponentType
            {
                get
                {
                    return this._objPropertyCollection.GetType();
                }
            }
    
            public override string DisplayName
            {
                get
                {
                    return this._objPropertyCollection[_strKey].DisplayName;
                }
            }
    
            public override string Name
            {
                get
                {
                    return this._objPropertyCollection[_strKey].Name;
                }
            }
    
            public override string Category
            {
                get
                {
                    return this._objPropertyCollection[_strKey].Category;
                }
            }
    
            public override string Description
            {
                get
                {
                    return this._objPropertyCollection[_strKey].Description;
                }
            }
    
            public override bool IsReadOnly
            {
                get
                {
                    return this._objPropertyCollection[_strKey].ReadOnly;
                }
            }
    
            public override bool IsBrowsable
            {
                get
                {
                    return this._objPropertyCollection[_strKey].Browsable;
                }
            }
    
            public override object GetValue(object objComponent)
            {
                return this._objPropertyCollection[_strKey].Value;
            }
    
            public override void SetValue(object objComponent, object objValue)
            {
                this._objPropertyCollection[_strKey].Value = objValue;
            }
    
            public override Type PropertyType
            {
                get
                {
                    Type objType = Type.GetType(this._objPropertyCollection[_strKey].PropertyTypeName);
    
                    if (objType == null)
                    {
                        objType = typeof(System.String);
                    }
                    return objType;
                }
            }
    
            public override TypeConverter Converter
            {
                get
                {
                    if (this._objPropertyCollection[_strKey].ConverterTypeName == null)
                    {
                        return base.Converter;
                    }
                    else
                    {
                        Type objType = Type.GetType(this._objPropertyCollection[_strKey].ConverterTypeName);
                        return (TypeConverter)Activator.CreateInstance(objType);
                    }
                }
            }
    
            public override object GetEditor(Type editorBaseType)
            {
                if (this._objPropertyCollection[_strKey].EditorTypeName == null)
                {
                    return base.GetEditor(editorBaseType);
                }
                else
                {
                    Type objType = Type.GetType(this._objPropertyCollection[_strKey].EditorTypeName);
                    return Activator.CreateInstance(objType);
                }
            }
    
            public override void ResetValue(object objComponent)
            {
                if (_bResetDefaultSwitch)
                {
                    this._objPropertyCollection[_strKey].Value = this._objPropertyCollection[_strKey].ResetValue;
                    _bResetDefaultSwitch = false;
                }
                else
                {
                    this._objPropertyCollection[_strKey].Value = this._objPropertyCollection[_strKey].DefaultValue;
                    _bResetDefaultSwitch = true;
                }
            }
    
            public override bool ShouldSerializeValue(object objComponent)
            {
                if (this._objPropertyCollection[_strKey].Value == this._objPropertyCollection[_strKey].DefaultValue)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
    


  • Das einfachste wäre aus meiner Sicht ein Objekt X, welches eine Liste der zu bindenden Daten enthält.

    Anschließend wird Objekt X an das PropertyGrid gebunden und der Zugriff auf die Untergeordneten Einträge ist möglich.



  • Hab ich versucht.

    CClass MyTestClass = new CClass();
                List<object> MyObjects = new List<object>();
                MyObjects.Add(MyTestClass);
                MyObjects.Add(MyTestClass.DynamicData);
                propertyGrid1.SelectedObject = MyObjects;
    

    Dann zeigt das PropertyGrid allerdings die Eigenschaften der Liste an, also count usw.

    Aber ist es möglich, eine statische Property-Definition zu der Liste DynamicData hinzuzufügen? Das wäre ein Workarround.



  • Einziges was mir jetzt einfallen würde, wäre von List<T> abzuleiten und in der abgeleiteten Klasse alle Properties außer die Items auszublenden mit deiner tollen Reflection-Funktion.


Anmelden zum Antworten