wpf-117149-controls-and-libraries-property-grid-expandability-customization.md
The PropertyGrid control allows you to customize property expandability in the following ways.
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.
<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>
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" }
}
};
}
}
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; }
}
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.
<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>
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;
}
}
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