I need to change the CellTemplate of a RadGridView. All of the examples I can find online defines the Columns statically in the Xaml, and then in that Column tag, they define the CellTemplate:
<telerik:RadGridView AutoGenerateColumns="False" ItemsSource="{Binding}" RowStyleSelector="{StaticResource styleSelector}">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn DataMemberBinding="{Binding ID}" CellTemplateSelector="{StaticResource templateSelector}" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
The above example loads the CellTemplate only for the column with the heading "ID", and does that for all of the cells in that column.
I have managed to load the CellTemplates in the code behind but this required me to pass an instance of the whole grid to the ViewModel, and also had to add a dependency Property to the GridView to bind the Columns of the grid to a GridViewColumnCollection in the ViewModel.
This is a very messy workaround, one that I'm sure will get me fired if seen by the wrong eyes. What I need to do is something similar to this:
<telerik:RadGridView.CellTemplateSelector>
<local:MyTemplateSelector>
<local:MyTemplateSelector.NormalTemplate>
<DataTemplate>
...Some [Normal] template...
</DataTemplate>
</local:MyTemplateSelector.NormalTemplate
<local:MyTemplateSelector.DropdownTemplate>
<DataTemplate>
...Some [ComboBox] template...
</DataTemplate>
</local:MyTemplateSelector.DropdownTemplate>
</local:MyTemplateSelector>
</telerik:RadGridView.CellTemplateSelector>
I honestly have no idea why this RadGridView makes it so difficult to change the CellTemplate, because this prevents you from changing a common property such as the Foreground Color in the Label found in the ContentTemplate in the Cell itself.. Any ideas as to what I can do?
<telerik:RadGridView AutoGenerateColumns="False" ItemsSource="{Binding}" RowStyleSelector="{StaticResource styleSelector}">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn DataMemberBinding="{Binding ID}" CellTemplateSelector="{StaticResource templateSelector}" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
The above example loads the CellTemplate only for the column with the heading "ID", and does that for all of the cells in that column.
I have managed to load the CellTemplates in the code behind but this required me to pass an instance of the whole grid to the ViewModel, and also had to add a dependency Property to the GridView to bind the Columns of the grid to a GridViewColumnCollection in the ViewModel.
This is a very messy workaround, one that I'm sure will get me fired if seen by the wrong eyes. What I need to do is something similar to this:
<telerik:RadGridView.CellTemplateSelector>
<local:MyTemplateSelector>
<local:MyTemplateSelector.NormalTemplate>
<DataTemplate>
...Some [Normal] template...
</DataTemplate>
</local:MyTemplateSelector.NormalTemplate
<local:MyTemplateSelector.DropdownTemplate>
<DataTemplate>
...Some [ComboBox] template...
</DataTemplate>
</local:MyTemplateSelector.DropdownTemplate>
</local:MyTemplateSelector>
</telerik:RadGridView.CellTemplateSelector>
I honestly have no idea why this RadGridView makes it so difficult to change the CellTemplate, because this prevents you from changing a common property such as the Foreground Color in the Label found in the ContentTemplate in the Cell itself.. Any ideas as to what I can do?