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

DataTemplateSelector, containing class of property?

2 Answers 83 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 28 Sep 2012, 12:53 AM
When I use a DataTemplateSelector, the type passed into SelectTemplate via the item parameter is PropertyDefinition. I can get things like the name of the property and the type of the property, but how about the type that contains the property? If I have five classes with a string property called "Reference" and I need a data template for each of those, I need the name of the class that contains that property to select the proper template. How do I obtain that information?

2 Answers, 1 is accepted

Sort by
0
Matt
Top achievements
Rank 1
answered on 01 Oct 2012, 11:51 PM
Hmm, nobody seems to have an answer to this yet. OK so how about another approach. Perhaps I can solve the problem by creating my own property definitions so I have control over what goes through the template selector. The thing is that my Property Grid isn't being used to edit one type but rather many types depending on what you select in a corresponding tree view. I'm using MVVM and in my view model classes I have a boolean property to indicate whether or not to auto-generate the property defs and then a second property for the property definitions themselves. The PropertyDefinitions property looks like it's read-only though, is there a proper way to databind this to my own collection?
0
Ivan Ivanov
Telerik team
answered on 03 Oct 2012, 07:20 AM
Hi,

 Basically a PropertyDefinition depends only on the underlaying property, so that it does not contain any public information about the type which member is that property. You may try storing the DataTemplateSelectorinstance in your ViewModel, so that the Item value is visible in its scope. Apparently, this is not a great MVVM approach. We have discussed this scenario and we have decided to expose the respectivePropertyGridField instance as "Container" parameter in the invocation of of DataTemplateSelector'sSelectTemplate(). As it is a visual element, you would be able to access RadPropertyGrid's Item easily. This change will be available with next week's internal build. As for the PropertyDefinitions property, it is ReadOnly by design. You can try using an attached behavior that subscribes to RadPropertyGrid's loaded event and adding the PropertyDefinitions there. 

All the best,
Ivan Ivanov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
PropertyGrid
Asked by
Matt
Top achievements
Rank 1
Answers by
Matt
Top achievements
Rank 1
Ivan Ivanov
Telerik team
Share this question
or