RadGridView CellTemplate on dynamic columns

7 posts, 0 answers
  1. Johan
    Johan avatar
    6 posts
    Member since:
    Jul 2015

    Posted 11 Jul 2015 Link to this post

    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?
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 14 Jul 2015 Link to this post

    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
  3. UI for WPF is Visual Studio 2017 Ready
  4. Johan
    Johan avatar
    6 posts
    Member since:
    Jul 2015

    Posted 14 Jul 2015 in reply to Dimitrina Link to this post

    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,

  5. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 15 Jul 2015 Link to this post

    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
  6. Joel Palmer
    Joel Palmer avatar
    164 posts
    Member since:
    May 2009

    Posted 11 Aug 2015 Link to this post

    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="*"/>

     
  7. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 12 Aug 2015 Link to this post

    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
  8. Joel Palmer
    Joel Palmer avatar
    164 posts
    Member since:
    May 2009

    Posted 12 Aug 2015 Link to this post

    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");
    }

Back to Top
UI for WPF is Visual Studio 2017 Ready