Multiselect and Selecting Parent Items

2 posts, 0 answers
  1. Valeriu
    Valeriu avatar
    21 posts
    Member since:
    Dec 2010

    Posted 26 Feb 2013 Link to this post

    Hello,

    The setup:
     - RadTreeView bound to an hierarchy.
     - SelectMode is Multiselect
     - Each node has an IsSelected property, RadTreeViewItem is bound to it using a style.

    And one of requirements is that when a child is selected then all his parents should be selected too. And we've run into a problem implementing it.

    Reproducing the problem:
     - Expand parent node without selecting
     - Click on a child
     - The Child is selected and his parent too
     - Click again on child
     At this moment parent is unselected by the TreeView, while child is still selected.

    Definition of IsSelected property:
    public bool IsSelected
    {
        get { return isSelected; }
        set
        {
            if (value == isSelected)
                return;
     
            isSelected = value;
            NotifyOfPropertyChange(() => IsSelected);
     
            if (Parent != null)
                Parent.IsSelected = isSelected;
        }
    }

    When clicking second time on a child, the IsSelected setter isn't called on a child, but sets to false for parent.

    Basically I'd like to keep a visual "selection chain" from children to parent nodes. Anything special should be done in order to keep parent nodes selected? 

    Thanks
  2. Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1183 posts

    Posted 01 Mar 2013 Link to this post

    Hi Valeriu,

    In your scenario you mix together the selection logic of the RadTreeView control with your custom logic and this is not working as expected. The multiple selection logic of the control is triggered when the Ctrl key is pressed. Hence when you click on an item for the second time, that logic is not triggered. However, I suggest you to use a CheckBox control to keep the selection of all RadTreeViewItems (parents and children). By doing so you will separate your custom selection logic and the selection logic of the RadTreeView control. Also you will be able to fully customize the selection logic.

    In order to implement this approach, each of your items should keep a reference to its parent item (as you already implemented). Also, you can implement methods which will handle the selection of the check boxes (the RadTreeView items respectively). Here you can find detailed information about the implementation of this approach and at the end of the article you can find a link to our CodeLibray section where a ready to use project can be downloaded.

    Please try it out and let me know if it works for you.

    Kind regards,
    Pavel R. Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
Back to Top