RadTreeView.GetItemByPath fails with hierarchical data binding

9 posts, 1 answers
  1. Bjorn Sundby
    Bjorn Sundby avatar
    5 posts
    Member since:
    Jun 2012

    Posted 14 Apr 2010 Link to this post

    I have a RadTreeView where I use data binding based on a HierarchicalDataTemplate.
    I need to expand the tree and select a node based on some action triggered elsewhere.
    I am able to calculate the path to the node and have tried to use the GetItemByPath function to expand the tree and get to the node.
    This function always returns null when I am using data binding.
    If I create the tree programatically by creating RadTreeViewItems then the function is working.

    Has anybody else experienced this and do they have a solution in the data binding case.

    I am using Windows XP SP 2, Visual Studio 2008 SP1, Silverlight 3.5, RadControls_for_Silverlight_2010_1_0309_DEV, Internet Explorer 7.0.5730.11.

    B.Sundby
  2. Answer
    Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 14 Apr 2010 Link to this post

    Hi Bjorn Sundby,

    I am not sure that I understand your scenario correctly. However, I prepared a sample project, demonstrating how you can search an item in the ItemsSource collection of the RadTreeView. When the targeted item is found, it is expanded, selected and checked. The RadTreeView ItemsSource collection is bound to an ObservableCollection using HierarchicalDataTemplate  and it works as expected on our side.

    Can you take a look at the project and let me know if it helps?

    Best wishes,
    Tina Stancheva
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft banner
  4. Bjorn Sundby
    Bjorn Sundby avatar
    5 posts
    Member since:
    Jun 2012

    Posted 14 Apr 2010 Link to this post

    Thank you for Your quick response.

    I was able to find my problem based on Your working sample.
    It turned out that the data source model requires an override for the ToString method to let the TreeView know the name of the node.
    So by adding the following code to my hierarchical data model item class; everything worked fine.

     

     

    public override string ToString()  
    {  
        return this.Name;  
    }  
     


    B.Sundby

     

  5. Aaron
    Aaron avatar
    6 posts
    Member since:
    Nov 2008

    Posted 14 Jul 2010 Link to this post

    Hello Tina,

    I have downloaded and run the project you attached in April, and I am able to use it successfully, however I can't seem to apply the same method to my project. GetItemByPath always returns null, for me. I can use ContainerFromItemRecursive, however that only works when the item has been rendered, visually.

    I have a few questions for you:
    • Can you expand a bit on how the GetItemByPath method works?
    • Does my business object absolutely need to override it's ToString() method to output it's Name or Display property? In the example project, I was able to form paths with the object's names rather than their ToString() result and successfully find them in the tree. Can you explain how this works, exactly?
    • In order for GetItemByPath to return the object, is it necessary for the hierarchical business objects to be grouped into an ObservableCollection<>? Or will a List<> satisfy? I am aware that an ObservableCollection<> is needed to fire changes on the business objects up to the UI control, however is it necessary for use with GetItemByPath?
    • What settings on the RadTreeView might disable or disrupt the use of GetItemByPath? Is there some Style Property or Template Property that when altered my render the GetItemByPath method useless?
    • What steps must I take to ensure that GetItemByPath will find my business object?

    Thanks in advance for answering my questions,
    - Aaron
  6. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 20 Jul 2010 Link to this post

    Hi Aaron,

    When using the GetItemByPath() method with databound RadTreeView there are two approaches. The first approach is to override the ToString() method. Its value will be used as path segment. The second approach is to use the telerik:TextSearch.TextPath attached property to indicate which property will be used for matching.

    I will try to explain the GetItemByPath() method in more details, hopefully that will enable you to find the cause for your issues.

    The method takes the string that is passed through the path parameter and searches through the RadTreeView Items collection to find the first match. The matching mechanism splits the path into parts using the provided separator and compares it to the value of each TreeViewItem. Then if a RadTreeView item is a match, the method traverse the children of that item for a match to the next part of the path and so on.

    However, when the RadTreeView is databound, the expression that is used to evaluate each RadTreeViewItem will return different values depending on whether a TextSearch.TextPath attached property is set and if it isn't -  what does the ToString() method of the corresponding business classes return.
    If the TextSearch.TextPath property isn't set and the ToString() method isn't overridden, the expression used to evaluate each RadTreeViewItem in the matching mechanism, will return the name of the corresponding business class, for example _289938_TreeViewContainerBindings.Company and will use that value to match it with the path parameter. Therefore if the passed path parameter is for example comp.Name, the GetItemByPath() method won't find any matches. And, if you pass comp.ToString()(=_289938_TreeViewContainerBindings.Company) as a path, the method will compare two identical strings at all times and won't return the corresponding to the path RadTreeViewItem, but the first match.

    However, if the ToString() method is overridden, for example to return the Name property of the business class, the expression used to evaluate each RadTreeViewItem in the matching mechanism, will return also the Name property. Therefore as the path can be passed both comp.ToString() and comp.Name to the GetItemByPath() method to get the corresponding RadTreeViewItem.

    In the second approach, the value of the  TextSearch.TextPath property will be used as a path segment. I modified the example project to illustrate this approach so that you can see how it works and choose the best suited approach for you. Also, in the modified project all collections are of type List which actually doesn't affect the result of the GetItemByPath() method.

    I hope this information will help you. However, if you need more info, please let me know and I will be more than happy to further assist you.

    Best wishes,
    Tina Stancheva
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Sameeksha
    Sameeksha avatar
    4 posts
    Member since:
    Sep 2010

    Posted 11 Oct 2010 Link to this post

    Great! Container bindings work great!
  8. Bala
    Bala avatar
    20 posts
    Member since:
    Dec 2011

    Posted 05 Mar 2012 Link to this post

    hi ,

     could be plz describe in case of there is replicated names .... which node will expand ....
  9. Bala
    Bala avatar
    20 posts
    Member since:
    Dec 2011

    Posted 05 Mar 2012 Link to this post

    Hi Tina Stancheva ,
       
       i m following this sample(http://www.telerik.com/community/forums/silverlight/treeview/bind-radtreeview-to-xml.aspx) as my db record is bulk i m shifting to XML . 

    But i m unable to select the particular node - either using getitembypath() and expanditembypath() - So could u guide me with this procedure.


    waiting for ur Guidence---

     
  10. Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 06 Mar 2012 Link to this post

    Hello,

    If there are items with duplicated name the first occurrence (from top to bottom) will be expanded.

    Regarding your most recent post: I've posted a reply in the following threads:
    http://www.telerik.com/community/forums/silverlight/treeview/treeview---getitembypath-using-xml-binding.aspx 
    and
    http://www.telerik.com/community/forums/silverlight/treeview/bind-radtreeview-to-xml.aspx .

    Don't hesitate to contact us if you need more info or have any questions.

    Regards,
    Hristo
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top
DevCraft banner