Accessing Additional Templated Controls in RadTreeViewItem

6 posts, 2 answers
  1. Mark
    Mark avatar
    3 posts
    Member since:
    Oct 2014

    Posted 14 Nov 2014 Link to this post

    I'm using a ControlTemplate to add a couple of additional Controls (an Image and a TextBlock) to RadTreeViewItem. I'm also using a HierarchicalDataTemplate and data binding to attach the RadTreeView to my data.

    With this, I'm unable to find any way to bind these additional controls to data, or access the additional controls in code. If I were creating the TreeViewItems myself, I think I'd create a subclass of RadTreeViewItem and add a couple of properties that I could bind the new controls to.

    What I ended up doing was add the following to RadTreeViewItem:
    /// <summary>
    /// Exposes protected GetTemplateChild
    /// </summary>
    public DependencyObject PublicGetTemplateChild(string childName)
    {
        //
        //  In the base control class GetTemplateChild is protected. I need to open it up for
        //  RadTreeViewItem so we can find elements that we added in data templates
        //
        return GetTemplateChild(childName);
    }

    This allows me to find my child controls in code when the RadTreeViewItem is loaded and then bind my data to them.

    Is there a better way to do this?

    Is it possible to get the RadTreeView to create my sub-classed RadTreeViewItem, rather than a RadTreeViewItem? It looks like it isn't, but if there's no better way to do this, would it be worth adding?


  2. Answer
    Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1182 posts

    Posted 19 Nov 2014 Link to this post

    Hello Mark,

    Do you mind sharing with us why you need to customize the ControlTemplpate of the RadTreeViewItem to achieve your requirement? Is it not possible to create custom DataTempalte and assign it to the RadTreeViewItem.ItemTemplate? Please keep in mind that if our customers change the default ControlTemplates of our controls we will no longer support those changes. This is why it would better to use custom DataTemplate instead of changing the ControlTemplate.

    Working with custom DataTemplate allows you to use bindings. This way you will be able to control the additional UI components through your business items. At this point  I cannot see any benefits in customizing the default ControlTempalte of the RadTreeViewItem in your particular scenario.

    Basically, you can define the same visual structure with a DataTemplate. Could you please send me a picture of the result that you need to achieve and I will try to create a DataTemplate achieving your requirement for you? 

    Regards,
    Pavel R. Pavlov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. DevCraft banner
  4. Mark
    Mark avatar
    3 posts
    Member since:
    Oct 2014

    Posted 19 Nov 2014 in reply to Pavel R. Pavlov Link to this post

    Pavel,

    Thanks for the reply. 

    >>Is it not possible to create custom DataTempalte and assign it to the RadTreeViewItem.ItemTemplate?
    That's what I was hoping to hear (or at least something like it). It seemed I was going about this the wrong way. All the examples I saw for DataTemplate/ItemTemplate seemed to be binding data to the existing TreeViewItem layout. Looking closer at http://www.telerik.com/help/silverlight/radtreeview-populating-with-data-item-template.html, I see that the controls are being defined directly in the DataTemplate, they just happen to look like a normal TreeViewItem.

    I don't know how I got off on the ControlTemplpate tangent, I think I found some samples that used it to change the appearcne of the treeview and it wasn't apparent to me that the DataTemplate/ItemTemplate contains controls.

    I'll give it another shot.

    Attached is a first stab at what I'm trying to accomplish. Several images bound to different properties (fileType,dirtyFlag and versionStatus), and 2 blocks of text bound to different proprties (name and status). All 1-way bindings. The tree isn't editable.
  5. Mark
    Mark avatar
    3 posts
    Member since:
    Oct 2014

    Posted 19 Nov 2014 in reply to Mark Link to this post

    I've got it working with the DataTemplate/ItemTemplate and data bindings.

    Thanks for pointing me in the right direction.
  6. Answer
    Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 24 Nov 2014 Link to this post

    Hello Mark,

    The following samples and help articles also demonstrate custom DataTemplates used for ItemTemplate of the RadTreeView. Please let us know if you need further assistance.

    RadTreeView How to Implement Tri State CheckBox with MVVM
    RadTreeView First Look Demo 
    RadTreeView HierarchicalDataTemplate Demo

    Regards,
    Petar Mladenov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  7. Mark
    Mark avatar
    3 posts
    Member since:
    Oct 2014

    Posted 24 Nov 2014 in reply to Petar Mladenov Link to this post

    The tri-state example is it exactly. I don't know how I missed it. Thanks.
Back to Top
DevCraft banner