How to set HeaderTemplateSelector in code behind?

4 posts, 0 answers
  1. s2uiguy
    s2uiguy avatar
    24 posts
    Member since:
    Jan 2011

    Posted 12 Jul 2011 Link to this post

    Hi - I am adding a number of the RadTabItems in my RadTabControl dynamically and I would like to set the HeaderTemplateSelector for SOME of these new RadTabItems but not all.

    How can I set the HeaderTemplateSelector in the code behind?

    This is what I'm trying and it doesn't seem to be working.  Thanks in advance for any help!

    ...
    newTabItem.HeaderTemplateSelector = new HeaderSelector()
    ...
     public class HeaderSelector : DataTemplateSelector {
        public override DataTemplate SelectTemplate(object item, DependencyObject container) {
          return Application.Current.Resources["AlarmTabItemDataTemplate"] as DataTemplate;
        }
      }
  2. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 14 Jul 2011 Link to this post

    Hi Fortpointuiguy,

    The HeaderTemplateSelector property of the RadTabItem is exposed to allow you to apply a different DataTemplates to a databound RadTabItem. This property is mostly used in MVVM scenarios where the RadTabControl ItemsSource is bound to a business collection.

    Also, when you create a custom DataTemplateSelector, you don't have to apply it only to certain items since the main purpose of this class is to allow you to evaluate each RadTabItem and apply to it a HeaderTemplate based on that evaluation. So instead of setting a DataTemplateSelctor to a few items, you can apply it to all and implement custom logic to choose different templates for each item. And for such scenarios, you can take advantage of the RadTabControl.ItemTemplateSelector property. It applies different HeaderTemplates to the RadTabControl Items - the same as the HeaderTemplateSelector.

    Keeping the above in mind, you can implement your scenario by binding the RadTabControl to a business collection and setting its ItemTemplateSelector property. In the custom HeaderSelector class you can implement custom logic to choose different templates for the items based on your business logic. Also, when you need to add new items to the RadTabControl, you can simply add new business items to the business collection.

    However, if you need to create RadTabItems in code-behind, then you don't have to use the HeadertemplateSelector since you don't have to set any bindings to the Header property. You can set the Header in the code.

    I attached a sample project illustrating a scenario where the RadTabControl is data bound to a business collection and the ItemTemplateSelector property is used to define different HeaderTemplates for the RadTabItems. Please have a look at it and let us know if it helps or if you need more info.

    All the best,
    Tina Stancheva
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

  3. DevCraft banner
  4. s2uiguy
    s2uiguy avatar
    24 posts
    Member since:
    Jan 2011

    Posted 14 Jul 2011 Link to this post

    Tina - thanks for the reply.

    This is helpful.

    I think the reason why I'm struggling a bit is because I have one tab that is not associated with the collection of data and because it is not bound to the same data collection it cannot be used with the same selector.

    In order to help you visual this I might have a data collection with Item1, Item2, Item3 but I want my tab control to look as follows:

    General     Item1    Item2   Item3

    where the General tab is a "special" tab that is not in the data collection.

    Is there a way to handle a case such as this?

    Thanks,

    FortPointUIGuy
  5. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 19 Jul 2011 Link to this post

    Hello Fortpointuiguy,

    I guess you need to use CompositeCollection: a static RadTabItem and a bindable collection. This cannot be achieved in RadTabControl. You can vote for this feature request in our PITS
    Feel free to ask if you need further info.

    Best wishes,
    Petar Mladenov
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Back to Top