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

set cell template in code behind

2 Answers 109 Views
GridView
This is a migrated thread and some comments may be shown as answers.
jen
Top achievements
Rank 1
jen asked on 12 Jul 2017, 07:13 PM

I am attempting to set a column's cell template in code to a template that is also generated in code, but its not working as I assumed it would.
The template seems to only be applied to one row at a time, always the last row to draw. I suspect this is because its an instance of a template and not a resource, but I'm not sure the right way to set this up.

I am making a custom search control that displays results in a grid; I need to add a button to a column and hook the click event up. It is much easier for me to set up in code in this case.

 

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();

   //find grid column control part
    GridViewColumn PART_SelectColumn = GetTemplateChild("PART_SelectColumn") as Telerik.Windows.Controls.GridViewColumn;

    //make cell content
    RadButton btn_SelectItem = new RadButton() { Content = "Add"};
    btn_SelectItem .Click += PART_SelectItem_Click;

    //generate template - code builds a sealed datatemplate with the button as the content
    DataTemplate SelectTemplate = DataTemplateExtentions.CreateDataTemplate ( () => { return btn_SelectItem }  );

    //Set the column's cell template - I suspect this is what I'm doing wrong
    PART_SelectColumn.CellTemplate = SelectTemplate;

}

 

see attached image; the template only applies to the last row. 

Anyone know where I'm going wrong?

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Dilyan Traykov
Telerik team
answered on 17 Jul 2017, 10:29 AM
Hello jen,

Could you please clarify which OnApplyTemplate method you're overriding and why?

As you cannot register for events in DataTemplates, I would recommend using the approach suggested in this article instead and create a custom button column.

Would such an approach be applicable for your requirements? 

Regards,
Dilyan Traykov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
jen
Top achievements
Rank 1
answered on 17 Jul 2017, 02:58 PM
Thanks, the custom column approach worked well
Tags
GridView
Asked by
jen
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
jen
Top achievements
Rank 1
Share this question
or