Allow only one child

2 posts, 0 answers
  1. Gilbert van Veen
    Gilbert van Veen avatar
    68 posts
    Member since:
    Oct 2009

    Posted 01 Apr 2010 Link to this post

    Hello Telerik friends,

    I have the following situation:

    Base -
              - A
              - B
              - C
              - D

    This is the default treeview. Base is the root and A .. D are childs. I want the user to be able to move/reorder the childs around but they may not be moved to the root level (Base).  Every other child should be a child of a parent. Also each child can max. have only one other child. See below.

    Valid structure:
    Base -
               - A
                   - B
                       - C
                          - D


    Invald:
    Base -
               - A
               - B
                       - C
                           - D

    Also invalid:
    Base -
               - A
                   - B
                       - C
                       - D
    Base -
               - A
                   - B
                       - C
                       - D

    In other words every child may be only a child of a parent. The "main" parent is Base, Is this possible?
  2. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 06 Apr 2010 Link to this post

    Hi Gilbert Van Veen,

    In your case you have to handle the OnDropQuery() event and make sure that the items are dropped inside another item. You can do that by using the DropPosition property of the RadTreeViewItem.
     
    if you also want to make sure that the destination item doesn't have other children  you can use the RadTreeViewItem HasItems property, and to check that the destination item isn't the root item of the tree you can use the RadTreeViewItem Level property, which is 0 only for the root items.

    private void OnDropQuery(object sender, DragDropQueryEventArgs e)
        {
            RadTreeViewItem item = e.OriginalSource as RadTreeViewItem;
            var dropPosition = item.DropPosition;
                 
            if (dropPosition == DropPosition.Inside && !item.HasItems && item.Level != 0)
            {
                e.QueryResult = true;
                e.Handled = true;
            }
            else e.QueryResult = false;
        }

    I have prepared an example for you. Please take a look at it and let me know if it works for you.


    Kind regards,
    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
Back to Top