Rob Smyth

Tuesday 25 June 2019

Telerik PropertiesGrid - Only show properties with attributes

To show only properties that have an explicit Display attribute in a Telerik PropertiesGrid (aka RadPropertisGrid) ...

Firstly, add the property gird with AutoGeneratePropertyDefinitions set to true and the AutoGeneratingPropertyDefinition event handler:

    <telerik:RadPropertyGrid AutoGeneratePropertyDefinitions="True"
                             Item="{Binding Selected}"

In the code behind:

    private void OnPropertyGridPropertyDefinition(object sender, AutoGeneratingPropertyDefinitionEventArgs e)
        var attributes = e.PropertyDefinition.PropertyDescriptor.Attributes;
        foreach (Attribute attribute in attributes)
            if (attribute is DisplayAttribute)
                e.Cancel = false;

        e.Cancel = true;

In the view model:

    public class MyViewModel : NodeViewModelBase
        Display(Name = "Name", GroupName = "Documentation", Description = "An optional name for this function block.", Order = 0)]
        public string Name { get; set; }