This is a migrated thread and some comments may be shown as answers.

Auto generating rows based on attributes

3 Answers 22 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 22 Jun 2018, 07:51 AM

Hello,

PropertyGrid looks promising but I'm struggling to make it work as I'd like. I want to be able to specify properties to display based on annotations in my View Model. Is this possible? I have tried applying the [Browsable] attribute and this works to an extent but you have to explicitly set [Browsable(fase)] on properties you want to exclude. In my case the View Model inherits from another class, so it is including all the properties from the base class and it is not my code so I can't add annotations to control these.

Is there any way to 'opt-in' to displaying properties? i.e. Only properties that are annotated will be displayed.

 

Thanks

3 Answers, 1 is accepted

Sort by
0
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 27 Jun 2018, 07:31 AM
Hello Chris,

What you could try is to subscribe to the AutoGeneratingPropertyDefinition event of the RadPropertyGrid. In the event handler, you can get the attributes set to the property and if given attribute is set you can cancel the generating of the field.

I have created sample project which demonstrates this approach. The project is attached to this reply.

Regards,
Dinko
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Chris
Top achievements
Rank 1
answered on 27 Jun 2018, 08:17 AM

Thanks Dinko. Based on your code I came up with this which works better for me as I have inheritance so not all my objects are of the same concrete type:

private void RadPropertyGrid_AutoGeneratingPropertyDefinition(
    object sender,
    AutoGeneratingPropertyDefinitionEventArgs e)
{
    var property = e.PropertyDefinition.PropertyDescriptor;
    var browsable = property.Attributes.OfType<BrowsableAttribute>().FirstOrDefault();
 
    if (browsable == null || browsable.Browsable == false)
    {
        e.Cancel = true;
    }
}

 

Thanks

Chris

0
Frank
Top achievements
Rank 1
answered on 22 Oct 2019, 08:51 AM
Nice point of discussion.
Tags
PropertyGrid
Asked by
Chris
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
Chris
Top achievements
Rank 1
Frank
Top achievements
Rank 1
Share this question
or