Tree will not expand to selection, unless it has been expanded to the level previously

6 posts, 0 answers
  1. Dmitri
    Dmitri avatar
    3 posts
    Member since:
    Jul 2013

    Posted 22 May 2014 Link to this post

    As the title suggest I have the following treeview:

    <telerik:RadTreeView  IsEditable="False"  ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" IsLineEnabled="True" IsRootLinesEnabled="False" Background="White" telerik:AnimationManager.IsAnimationEnabled="False"  IsVirtualizing="False"  IsLoadOnDemandEnabled="False"  AutoScrollToSelectedItem="True"/>


    Everything works as expected, except when the SelectedItem changes the tree won't expand to the level, unless that level has been navigated to previously, but the item itself still gets selected.

    I was under the impression that by setting IsVirtualizing and IsLoadOnDemandEnabled to False would have cleared this issue, but no change in behavior was observed.
  2. Martin
    Admin
    Martin avatar
    1099 posts

    Posted 27 May 2014 Link to this post

    Hello Dmitri,

    Note that when you select an item programmatically and this item haven't been expanded yet, the tree won't scroll to it. This is because the item's container is not yet generated. In order to scroll to the selected item you can use the SelectItemByPath() or BringPathIntoView() methods of the RadTreeView. 

    To use this approach basically you will need to add a string property in your business object that holds the path to the element.

    public class MyDataItem
    {
        ........
        public string Path { get; set; }
          public string Header { getset; }
    }
    Then set the TextSearch.TextPath attached property on the treeview. The value of the attached property should be set to the name of the property that is used for constructing the path.

    <telerik:RadTreeView TextSearch.TextPath="Header" />
    Here is an example with the SelectItemByPath():
    var vm = this.DataContext as ExampleViewModel;
    MyDataItem item = vm.Items.First().Children[1];
    var path = item.Path;
    this.Tree.SelectItemByPath(path);

    For your convenience I attached a sample project with the described approach. Please let me know if it works for you.

    Regards,
    Martin
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Dmitri
    Dmitri avatar
    3 posts
    Member since:
    Jul 2013

    Posted 27 May 2014 in reply to Martin Link to this post

    Is it possible to load the entire tree? There are certain objects I have no direct ability to inherit or override.
  5. Martin
    Admin
    Martin avatar
    1099 posts

    Posted 28 May 2014 Link to this post

    Hello Dmitri,

    Keep in mind that I don't recommend you to pre-load the entire treeview, because in a scenario with big amount of items this approach will be slow. This is why I can suggest you another option that you can use. Basically you can create wrappers for your objects and pass them to the tree instead of your original models. This will allow you to create additional properties and behavior for the business objects, therefore you will be able to use the approach with the SelectItemByPath() method. 

    Please give approach a try and let me know if it helps.

    Regards,
    Martin
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  6. Dmitri
    Dmitri avatar
    3 posts
    Member since:
    Jul 2013

    Posted 28 May 2014 in reply to Martin Link to this post

    It's a small but potentially "deep" tree I was hoping for the quickest solution, so I was hoping to avoid wrapping dozens of potential objects.
  7. Martin
    Admin
    Martin avatar
    1099 posts

    Posted 31 May 2014 Link to this post

    Hello Dmitri,

    You can load the entire tree if you use the ExpandAll() method (then CollapseAll() to collapse the nodes again). Please note that this method iterates recursively through the treeview and creates an container for each item in the ItemsSource. This might be a slow operation if your tree has many items (including in depth). That's the reason why I suggested you to use wrappers. You will need to write little more code but it will allow you to use the SelectItemByPath() method and in scenarios with big amount of data this will greatly improve the performance. 

    In addition would you mind telling me how deep your treeview can get (maximum levels you can have)? Thank you in advance for the information.

    Regards,
    Martin
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top
UI for WPF is Visual Studio 2017 Ready