I want to show nested properties under a property.

Grids for WPF Forum

Posted 7 months ago by Jiho Kim - Ansys
Version: 23.1.4
Avatar
public enum ExampleEnum
{
    Value1,
    Value2,
    Value3,
}

public class Source
{
    public ExampleEnum A { get; set; }
    public ExampleEnum A_1 { get; set; }
    public ExampleEnum A_2 { get; set; }
}

If class is defined as above and bind an instance of class to the propertygrid, is it possible that A is the root and A_1, A_2 are children of A and are Expandable?

In the propertygrid, A_1 and A_2 should look like children of property A, and when A is collapsed, A_1 and A_2 should be hidden.

https://www.actiprosoftware.com/docs/controls/wpf/api/actiprosoftware.windows.controls.grids.propertydata.idatamodel#actiprosoftware_windows_controls_grids_propertydata_idatamodel_children

I tried adding it to Children in the DataFactory, but it didn't show up the way I wanted it to.

I would like to be able to set the parent, child relationship when configuring the model in DataFactory.

 
 

[Modified 7 months ago]

Comments (2)

Answer - Posted 7 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

This sample shows an instance of using a custom data factory.  One of the examples in that sample is moving the phone numbers from a nested level up to the main level.  That's the opposite of what you are trying to do but is effectively a similar setup where you override the data factory's GetPropertyModels method to supply results.

The default data factory will examine the IPropertyModel.Converter to see if the TypeConverter there allows for sub-propeties.  Thus simply adding to the IPropertyModel.Children collection won't work unless you also update logic in the data factory's GetPropertyModels method.

In that sample mentioned above I made two changes as a test.

1) This change addes the voice phone number to the children of the previously-defined "propertyModel", which is for "CountryName". 

// Add voice phone number (routed from child object)
propertyModel.Children.Add(this.CreatePropertyModel(customer.PhoneNumbers, phoneNumbersPropertyDescriptors["Voice"], request));

2) This addition nearer the bottom of the method looks for the "CountryName" property model and returns its Children values directly, avoiding the base method call logic that looks at type converters:

else if (request.Parent?.Name == "CountryName") {
	return request.Parent.Children.OfType<IPropertyModel>().ToList();
}

With those two code changes, you should see the voice phone number appear as a sub-propety of country name within the property grid.  That is the concept you are trying to achieve.


Actipro Software Support

Posted 7 months ago by Jiho Kim - Ansys
Avatar

Great! We've implemented the feature with your suggestions.

I'm sharing the code in case anyone else is wondering the same thing.
ParentPropertyAttribute, ChildPropertyAttribute to determine the hierarchy of properties. Note the omitted extension methods.

protected override IPropertyModel CreatePropertyModel(object target, PropertyDescriptor propertyDescriptor, IDataFactoryRequest request)
{
    var propertyModel = base.CreatePropertyModel(target, propertyDescriptor, request);

    if (propertyDescriptor.HasParentPropertyAttribute(out var propertyDescriptors))
    {
        foreach (var childPropertyDescriptor in propertyDescriptors)
        {
            propertyModel.Children.Add(base.CreatePropertyModel(target, childPropertyDescriptor, request));
        }
    }
    else if (propertyDescriptor.HasAttribute<ChildPropertyAttribute>())
    {
        return default;
    }

    return propertyModel;
}

protected override IList<IPropertyModel> GetPropertyModels(object dataObject, IDataFactoryRequest request)
{
    if (request.Parent is CachedPropertyModelBase propertyModel)
    {
        if (propertyModel.HasAttribute<ParentPropertyAttribute>() &&
            propertyModel.Children.Count > 0)
        {
            return propertyModel.Children.OfType<IPropertyModel>().ToList();
        }
    }

    return base.GetPropertyModels(dataObject, request);
}
The latest build of this product (v24.1.4) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.