CellTemplateSelector and CellEditTemplateSelector
This article will show you how to conditionally apply a different data template to RadGridView cells using the CellTemplateSelector and CellEditTemplateSelector properties.
To learn the basics about TemplateSelectors please check this article.
Assume we have a GridView bound to a collection of clubs. Each club has a property StadiumCapacity. What we want to achieve is to apply one data template if the capacity is greater than 50 000 and another otherwise:
To do so follow these steps:
1. Create a new class which inherits the DataTemplateSelector class.
2. Override its SelectTemplate method. Based on your conditions - you return the proper DataTemplate that will be applied to the framework element (cell in our case).
In this case we have two different DataTemplates that could be applied - bigStadium and smallStadium. Depending on the underlying data we choose / select which template to apply.
3. In the XAML file define the template selector as a resource and set the properties of the bigStadium and smallStadium:
4. Finally, set the CellTemplateSelector property of the data column which represents the StadiumCapacity field:
You can use the CellEditTemplateSelector property in a similar manner with the only difference that the custom controls you define in the template will be displayed only once it enters edit mode.
Since the virtualization of the GridView is turned on by default, it is not recommended to work with the visual elements (i.e. GridViewCell) and their properties. You should not set properties of the GridViewCell inside the SelectTemplate method. Please check this help article for a reference.