How to refresh TreeView with same expanded nodes and selection

2 posts, 0 answers
  1. Björn
    Björn avatar
    23 posts
    Member since:
    Mar 2011

    Posted 25 Aug 2011 Link to this post

    Hi
    I have a treeview binded to hierarchical objects with four levels: TrainingProgram/Period/Subject/Teachingunit.
    The user can expand/collapse the tree.
    I want to refresh TreeView after reloading the data (because some operations are done directly on the server and then a reload is required).

    Everything works perfect except the selection of the last select item before reloading:
    CourseRadTreeView.SelectedItem = lastSelectedItem;

    I found the article with the idea of the IsSelected property, but this is also not working,
    because after reloading the selection in the property was loosing:
    http://www.telerik.com/support/kb/silverlight/treeview/radtreeview-and-hierarchicaldatatemplate.aspx

    What to do?

    Thanks
    Björn.

    XAML:
    <telerik:HierarchicalDataTemplate x:Key="Teachingunit">
        <Grid>
            <StackPanel Orientation="Horizontal" Grid.Column="0" HorizontalAlignment="Left">
                <TextBlock Text="{Binding StartDatetime, StringFormat='ddd dd.MM.yyyy,  HH:mm'}" />
            </StackPanel>
            <!-- Functions ... -->
        </Grid>
    </telerik:HierarchicalDataTemplate>
             
    <telerik:HierarchicalDataTemplate
        x:Key="Subject"
        ItemTemplate="{StaticResource Teachingunit}"
        ItemsSource="{Binding Path=TeachingunitSortedCollection.View}">
        <Grid>
            <TextBlock Text="{Binding Designation}" HorizontalAlignment="Left"/>
            <!-- Functions ... -->
        </Grid>
    </telerik:HierarchicalDataTemplate>
             
    <telerik:HierarchicalDataTemplate
        x:Key="Period"
        ItemTemplate="{StaticResource Subject}"
        ItemsSource="{Binding Path=SubjectSortedCollection.View}">
        <Grid>
            <TextBlock Text="{Binding Designation}" HorizontalAlignment="Left"/>
            <!-- Functions ... -->
        </Grid>
    </telerik:HierarchicalDataTemplate>
             
    <telerik:HierarchicalDataTemplate
        x:Key="TrainingProgram"
        ItemTemplate="{StaticResource Period}"
        ItemsSource="{Binding Path=PeriodSortedCollection.View}">
        <Grid>
            <TextBlock Text="{Binding Designation}" HorizontalAlignment="Left"/>
            <!-- Functions ... -->
        </Grid>
    </telerik:HierarchicalDataTemplate>
             
    <telerikNav:RadTreeView x:Name="CourseRadTreeView"
        IsLineEnabled="True" SelectionMode="Single" IsEditable="False"
        ItemsSource="{Binding Path=CourseDataSource}"              
        ItemTemplate="{StaticResource TrainingProgram}"
        Loaded="CourseRadTreeView_Loaded"
        Selected="CourseRadTreeView_Selected"
        Expanded="CourseRadTreeView_ExpandedCollapse" Collapsed="CourseRadTreeView_ExpandedCollapse">
    </telerikNav:RadTreeView>

    XAML.cs:
    public partial class CourseTreeView : UserControl
    {
        public CourseTreeView()
        {
            InitializeComponent();
     
            // when the data are reloaded update the treeview
            AppMessages.ReloadedOperationMessage.Register(this, OnReloadedOperation);
        }
     
        private void OnReloadedOperation(bool loaded)
        {
            ReexpandNodes();
            ReselectedLastSelectedItem();
        }
     
        // ===================================================================
        #region Selected Item
     
        private ICourse _lastSelectedItem = null;
     
        private void CourseRadTreeView_Selected(object sender, RadRoutedEventArgs e)
        {
            var radTreeViewItem = e.Source as RadTreeViewItem;
            if (radTreeViewItem != null)
            {
                // expand the new selected item
                radTreeViewItem.IsExpanded = true;
     
                // remember the last selection
                _lastSelectedItem = radTreeViewItem.Item as ICourse;
                CourseDetailViewModel.OnSelectionChangingCommand(radTreeViewItem.Item as ICourse);
            }
        }
     
        private void ReselectedLastSelectedItem()
        {
            // TODO: bbee / 20110825 - is not highlighting the selected item!
            //if (_lastSelectedItem != null)
            //{
            //  CourseRadTreeView.SelectedItem = _lastSelectedItem;
            //}
        }
     
        #endregion
     
        // ===================================================================
        #region Expand/Collapse
     
        private readonly Dictionary<string, string> _expandedPaths = new Dictionary<string, string>();
     
        private void CourseRadTreeView_ExpandedCollapsed(object sender, RadRoutedEventArgs e)
        {
            var radTreeViewItem = e.Source as RadTreeViewItem;
            if (radTreeViewItem != null)
            {
                // returns the full path with the delimiter '\': used ToString() from the course items
                var path = radTreeViewItem.FullPath;
                if (string.IsNullOrEmpty(path))
                    return;
     
                if (radTreeViewItem.IsExpanded)
                {
                    if (!_expandedPaths.ContainsKey(path))
                    {
                        _expandedPaths.Add(path, "");
                    }
                }
                else
                {
                    if (_expandedPaths.ContainsKey(path))
                    {
                        _expandedPaths.Remove(path);
                    }
                }
            }
        }
     
        private void ReexpandNodes()
        {
            // don't listen to the expand/collapse event during expanding
            CourseRadTreeView.Expanded -= CourseRadTreeView_ExpandedCollapsed;
            CourseRadTreeView.Collapsed -= CourseRadTreeView_ExpandedCollapsed;
     
            foreach (var expandedPath in _expandedPaths)
            {
                var path = expandedPath.Key;
                CourseRadTreeView.ExpandItemByPath(path);
            }
     
            // listen again
            CourseRadTreeView.Expanded += CourseRadTreeView_ExpandedCollapsed;
            CourseRadTreeView.Collapsed += CourseRadTreeView_ExpandedCollapsed;
        }
     
        #endregion
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 30 Aug 2011 Link to this post

    Hi Björn,

     I still think that you can implement the mentioned approach which uses ContainerBindings. If you use ViewModels that have boolean property and this property doesn't change when reloading,  the RadTreeView will have the proper item selected when reloading. So, is it possible for you to use ViewModels to wrap the dataitems that come from the DB ( I guess you populate your three from DB) ?

    Greetings,
    Petar Mladenov
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  3. DevCraft banner
Back to Top