Greg
Greg

Reputation: 1629

How can i list properties of child objects in the WPF Tookit PropertyGrid?

I am working with the propertyGrid from Xceed WPF tookit property grid.

When I bind an object to the grid, it lists the nested classes within it using the ToString() method and is read-only. I would like it to also list the properties within these nested classes. Does property grid support this? Is there a better library for this?

e.g.

Class Parameters {
    public int number {get;set;}
    Public CustomVector vector1 {get; set;} 
    Public CustomVector vector2 {get; set;}
}

Class CustomerVector {
    public double a {get; set;}
    public double b {get; set;}
    public double c {get; set;}
    public double d {get; set;}
}

In this case it would just list 3 elements, returning the ToString() for vector1 and vector2. I would like it to let me edit a,b,c,d of both vectors.

Upvotes: 1

Views: 2558

Answers (2)

Etherman
Etherman

Reputation: 1867

We can also use the [ExpandableObject] attribute on the nested objects, but this means polluting the class with WPF/presentation attributes (you could wrap the data class in a UI model class to keep things separate).

If we simply want all nested objects in unknown object types to be exapandable we can do something like this:

private void PropGrid_PreparePropertyItem(object sender, PropertyItemEventArgs e)
{
    var prop = e.Item as PropertyItem;
    if (prop != null)
    {
        prop.IsExpandable = prop.PropertyType.IsClass;
    }
}

I can't seem to find an option to just get it to do this by default for any object.

Upvotes: 0

pushpraj
pushpraj

Reputation: 13679

here you go

<xctk:PropertyGrid SelectedObject="{Binding Parameters}">
    <xctk:PropertyGrid.PropertyDefinitions>
        <xctk:PropertyDefinition Name="number" />
        <xctk:PropertyDefinition Name="vector1"
                                 IsExpandable="true" />
        <xctk:PropertyDefinition Name="vector2"
                                 IsExpandable="true" />
    </xctk:PropertyGrid.PropertyDefinitions>
</xctk:PropertyGrid>

IsExpandable="true" enables the property editor to expand to sub properties

also you can customize the display value of root (vector1, vector2) by overriding ToString() method in CustomerVector class

eg

    public override string ToString()
    {
        return string.Format("{0}, {1}, {2}, {3}", a, b, c, d);
    }

result

result

Upvotes: 4

Related Questions