Programmatically create header template

2 posts, 1 answers
  1. Tim Weckx
    Tim Weckx avatar
    4 posts
    Member since:
    Jul 2012

    Posted 27 Jul 2012 Link to this post


    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,

  2. Answer
    Dimitrina avatar
    3769 posts

    Posted 30 Jul 2012 Link to this post

    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.

Back to Top