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

Programmatically create header template

1 Answer 410 Views
This is a migrated thread and some comments may be shown as answers.
Tim Weckx
Top achievements
Rank 2
Tim Weckx asked on 27 Jul 2012, 09:38 PM

I'm trying to programmatically assign a header template for a column. The header template is similar to the GridViewSelectColumn as it just would have a checkbox in it, but I need the column to be able to bind to a property. I did look at the blog post from Vlad, but it will not work for this particular situation.

Below is the code I have to create a custom template (which works with for an ordinary ListView), but I don't know how to programmatically assign it to the column. I tried setting it to the column.Header property since it's of type object, but that did not work.

FrameworkElementFactory factory = new FrameworkElementFactory(typeof(CheckBox));
factory.AddHandler(CheckBox.CheckedEvent, new RoutedEventHandler(GridViewAll_HeaderCheckBox_Checked));
factory.AddHandler(CheckBox.UncheckedEvent, new RoutedEventHandler(GridViewAll_HeaderCheckBox_Checked));
factory.SetValue(CheckBox.ToolTipProperty, "(De)Select All");
DataTemplate template = new DataTemplate();
template.DataType = typeof(bool);
template.VisualTree = factory;

GridViewCheckBoxColumn column = new GridViewCheckBoxColumn();
column.DataMemberBinding = new Binding(prefix + property.Name);
column.Header = template // <-- this does not work

Thank you,

1 Answer, 1 is accepted

Sort by
Telerik team
answered on 30 Jul 2012, 06:04 AM
Hi Tim,

 The column.Header is of type object and you could assign an CheckBox object for it directly. I would suggest you to check this forum thread showing different scenarios with custom select CheckBox column. The thread is for Silverlight but the same is applicable for WPF as well.

All the best,
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Asked by
Tim Weckx
Top achievements
Rank 2
Answers by
Telerik team
Share this question