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

RadGridView CellTemplate on dynamic columns

6 Answers 1332 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Johan
Top achievements
Rank 1
Johan asked on 11 Jul 2015, 08:15 PM
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?

6 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 14 Jul 2015, 11:19 AM
Hi,

Have you considered applying a CellTemplateSelector? You can also find an example available with WPF Demos.

Regards,
Dimitrina
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Johan
Top achievements
Rank 1
answered on 14 Jul 2015, 11:43 AM

Hello,

Thanks for your response - Yes I did try to modify the CellTemplate, but as I've stated, it looks lik (from all the examples I've found, even in the WPF Demos) the CellTemplate can only be applied in the XAML when static columns are being defined. When the columns are automatically added by binding to a dataset, I cannot specify the CellTemplate. I need to be able to specify the Cell Template on any cell, depending on the value of the corresponding value in the cell in the dataset that cell is bound to and I'm not sure how I can achieve that

Thanks,

0
Dimitrina
Telerik team
answered on 15 Jul 2015, 01:14 PM
Hi,

In case applying a CellTemplateSelector does not work for you, then would it be possible for you to isolate it in a demo project and send it to us in a new support ticket? You can also take a look at this blog post for a reference on how to isolate an issue. 

Regards,
Dimitrina
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Joel Palmer
Top achievements
Rank 2
answered on 11 Aug 2015, 11:10 PM

I see this question was never resolved.  It shouldn't take an example.  If I have a RadGridView and I set the AutoGenerateColumns="True" then how do I apply a DataTemplate?  As previously posted, the example shows how to do this when a column is specifically created like this.  However, in this case I won't be explicitly setting a column.

<telerik:GridViewDataColumn
    DataMemberBinding="{Binding Name, Mode=TwoWay}"
    CellTemplate="{StaticResource TreeNodeTemplate}"
    CellEditTemplate="{StaticResource TreeNodeEditTemplate}"
    IsReadOnly="False"
    Width="*"/>

 
0
Dimitrina
Telerik team
answered on 12 Aug 2015, 12:18 PM
Hi,

You can subscribe for the AutoGeneratingColumn event and then work with e.Column.


Regards,
Dimitrina
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Joel Palmer
Top achievements
Rank 2
answered on 12 Aug 2015, 04:07 PM

Yes, that worked.  Here is my code:

GridViewDataColumn col = (GridViewDataColumn)e.Column;
Binding binding = col.DataMemberBinding;
if (binding.Path.Path == "Name")
{
    col.CellTemplate = (DataTemplate)this.FindResource("TreeNodeTemplate");
}

Tags
GridView
Asked by
Johan
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Johan
Top achievements
Rank 1
Joel Palmer
Top achievements
Rank 2
Share this question
or