Back to Devexpress

Expandability Customization

wpf-117149-controls-and-libraries-property-grid-expandability-customization.md

latest6.0 KB
Original Source

Expandability Customization

  • Jun 07, 2019
  • 3 minutes to read

Overview

The PropertyGrid control allows you to customize property expandability in the following ways.

Customizing Property Expandability at the Control Level

The following PropertyGrid properties affect the expandability behavior.

Note

The PropertyDefinition.AllowExpanding property has higher precedence than the PropertyGridControl.AllowExpanding property.

The following table lists the available AllowExpanding property values and corresponding expandability modes.

|

AllowExpanding

property value

|

Property grid expandability behavior

| | --- | --- | |

Default

|

Expands properties according to their type converters.

| |

Force

|

Expands all properties.

| |

ForceIfNoTypeConverter

|

Expands all properties that don’t have a type converter.

| |

Never

|

Properties are not expandable.

|

The following example demonstrates the AllowExpanding property precedence.

xaml
<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>
...
<dxprg:PropertyGridControl Grid.Column="1" SelectedObject="{Binding Data}" ShowProperties="All" AllowExpanding="Force">
    <dxprg:PropertyDefinition Path="PublicInfo"/>
    <dxprg:CollectionDefinition Path="PrivateInfo" AllowExpanding="Never"/>
</dxprg:PropertyGridControl>
csharp
public class ViewModel {
    public Container Data { get; set; }
    public ViewModel() {
        Data = new Container
        {
            PublicInfo = new ClassWithProperties() { Id=0, Name = "Expandable"},
            PrivateInfo = new List<ClassWithProperties>
            {
                new ClassWithProperties() { Id=1, Name="Not expandable" },
                new ClassWithProperties() { Id=2, Name="Not expandable" }
            }
        };
    }
}
csharp
public class ClassWithProperties {
    public int Id { get; set; }
    public string Name { get; set; }
    public override string ToString() {
        return Name;
    }
}

public class Container {
    public ClassWithProperties PublicInfo { get; set; }
    public List<ClassWithProperties> PrivateInfo { get; set; }
}

Customizing Property Expandability at the Model Level

To control property expandability from the model, use the TypeConverter attribute.

Important

A type converter affects the expandability if the AllowExpanding property is set to Default or ForceIfNoTypeConverter.

Note

To learn more about the property attributes supported by the PropertyGrid control, see Property Attributes.

xaml
<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>
...
<dxprg:PropertyGridControl Grid.Column="1" SelectedObject="{Binding Data}" ShowProperties="All" AllowExpanding="Default">

    <!--No type converter, not expandable-->
    <dxprg:PropertyDefinition Path="Simple"/>

    <dxprg:PropertyDefinition Path="Expandable"/>

    <dxprg:PropertyDefinition Path="NotExpandable"/>

    <!--No type converter, expandable-->
    <dxprg:CollectionDefinition Path="Collection"/>

</dxprg:PropertyGridControl>
csharp
using System.ComponentModel;
...
public class ClassWithProperties {
    public int Id { get; set; }
    public string Name { get; set; }
    public override string ToString() {
        return Name;
    }
}

public class Container {
    public ClassWithProperties Simple { get; set; }
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public ClassWithProperties Expandable { get; set; }
    [TypeConverter(typeof(NotExpandableConverter))]
    public ClassWithProperties NotExpandable { get; set; }
    public List<ClassWithProperties> Collection { get; set; }
}

public class NotExpandableConverter : TypeConverter {
    public override bool GetPropertiesSupported(ITypeDescriptorContext context) {
        return false;
    }
}
csharp
public class ViewModel {
    public Container Data { get; set; }
    public ViewModel() {
        Data = new Container
        {
            Simple = new ClassWithProperties() { Id=0, Name = "Simple"},
            Expandable = new ClassWithProperties() { Id=1, Name = "Expandable"},
            NotExpandable = new ClassWithProperties() { Id = 2, Name= "Not expandable" },
            Collection = new List<ClassWithProperties>
            {
                new ClassWithProperties() { Id=3, Name="Item1" },
                new ClassWithProperties() { Id=4, Name="Item2" }
            }
        };
    }
}

See Also

Property Definitions

Collection Definitions

Property Attributes