Apply Binding on PropertyGrid GroupItem

Sep 24, 2012 at 7:10 PM

I want to display a list of variables Name/Value in read only mode. Each variable has a category and I want by default the category to be collapsed. I tried several ways to bind the property IsExpanded using GroupItem Expander, but the Style gets never applied. The current DataContext of that GroupItem is a CollectionViewGroupInternal. I have set the SelectedObject to an object of type ViewVars.

            <Style TargetType="{x:Type Expander}">
                <Setter Property="IsExpanded" Value="{Binding Path=Items[0].IsExpanded}"/>
            </Style>

Do you have an idea?

 

    public class ViewVar
    {
        public string Name { get; set; }
        public string VarValue { get; set; }
        public VariableType VarType { get; set; }
        public string VarCategory { get; set; }
        public bool IsExpanded { get; set; }
    }
public class ViewVars { private bool isExpanded; public List<ViewVar> Vars { get; set; } public ViewVars() { Vars = new List<ViewVar>(); } public bool IsExpanded { get { return false; } set { isExpanded = value; } } } public class Variables : ViewVars, ICustomTypeDescriptor, INotifyPropertyChanged { #region ICustomTypeDescriptor Members public String GetClassName() { return TypeDescriptor.GetClassName(this, true); } public AttributeCollection GetAttributes() { AttributeCollection ac = TypeDescriptor.GetAttributes(this, true); return ac; } 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[] attributes) { return TypeDescriptor.GetEvents(this, attributes, true); } public EventDescriptorCollection GetEvents() { return TypeDescriptor.GetEvents(this, true); } public object GetPropertyOwner(PropertyDescriptor pd) { return this; } public PropertyDescriptorCollection GetProperties(Attribute[] attributes) { return GetProperties(); } /// <summary> /// Called to get the properties of this type. /// </summary> /// <returns></returns> public PropertyDescriptorCollection GetProperties() { var attributes = new Attribute[0]; var properties = Vars.Select(v => new VarListPropertyDescriptor(v, v.Name, v.VarValue.GetType(), new Attribute[0])); return new PropertyDescriptorCollection(properties.ToArray()); } #endregion #region Implementation of INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (PropertyChanged == null) { return; } var eventArgs = new PropertyChangedEventArgs(propertyName); PropertyChanged(this, eventArgs); } private void NotifyToRefreshAllProperties() { OnPropertyChanged(String.Empty); } #endregion } public class VarListPropertyDescriptor : PropertyDescriptor { private readonly ViewVar viewVar; private readonly Type propertyType; private bool isExpanded; public VarListPropertyDescriptor(ViewVar _viewVar, string propertyName, Type _propertyType, Attribute[] propertyAttributes) : base(propertyName, propertyAttributes) { viewVar = _viewVar; propertyType = _propertyType; } public override AttributeCollection Attributes { get { return new AttributeCollection(null); } } public override bool CanResetValue(object component) { return true; } public override Type ComponentType { get { return typeof(ViewVars); } } public override string DisplayName { get { return viewVar.Name; } } public override object GetValue(object component) { return viewVar.VarValue; } public bool IsExpanded { get { return false; } set { isExpanded = value; } } public override string Category { get { return viewVar.VarCategory; } } public override bool IsReadOnly { get { return true; } } public override Type PropertyType { get { return propertyType; } } public override void ResetValue(object component) { } public override void SetValue(object component, object value) { viewVar.VarValue = value as string; } public override bool ShouldSerializeValue(object component) { return true; } }