Only allowing drop where it is appropriate within the hierarachy

4 posts, 1 answers
  1. David
    David avatar
    19 posts
    Member since:
    Jun 2008

    Posted 17 Jan 2011 Link to this post

    Hi there,

    I am trying to get drag and drop within a radtreeview to work for the following scenario without much luck after following a recent example posted in this forum.
    * Two level hierarchy: A list of Teams containing Player objects.
    * Teams and Players can be dragged.
    * Teams can only be dropped before or after Team objects
    * Players can be dropped inside a Team
    * Players can be rearranged within a Team
    * Players cannot be dropped inside of other Players

    I managed to get some of the above working, however an issue I ran into is this. I could not stop a Player being placed at the same level as a Team object, when that Player is dragged right at the bottom of the treeview.

    How do I prevent Player objects from being dragged to the root level?

    This is what I have:
    private void OnDropInsideTreeViewDropQuery(object sender, DragDropQueryEventArgs e)
    {
      var destination = e.Options.Destination as RadTreeViewItem;
      if (destination != null && e.Options.Status == DragStatus.DropDestinationQuery)
        if (destination.DropPosition == DropPosition.Inside)
        {
          Console.WriteLine("Inside");
          Team t= destination.Item as Team;
          if (t != null)
          {
            object item = (e.Options.Payload as Collection<Object>)[0];
            if (item.GetType().Name == "Team")
            {
              e.QueryResult = false;
            }
          }
        }
        else
        {
          object item = (e.Options.Payload as Collection<Object>)[0];
          Team t = destination.Item as Team;
          if (t!= null)
          {
            if (item.GetType().Name == "Player")
            {
              e.QueryResult = false;
            }
          }
          else
          {
            //
          }
    
    
    • How can I prevent the Player being dragged to the same level as the Team objects?
  2. Shawn
    Shawn avatar
    1 posts
    Member since:
    Jan 2011

    Posted 18 Jan 2011 Link to this post

    I have essentially the same goal as you, and it looks like you're farther along than I am. I am eagerly awaiting a response on this one.

    An in-depth answer on the best way to do this would be greatly appreciated.

    Edit:
    I found this thread and it was quite helpful:
    http://www.telerik.com/community/forums/wpf/treeview/drag-and-drop.aspx
  3. Answer
    Petar Mladenov
    Admin
    Petar Mladenov avatar
    2991 posts

    Posted 19 Jan 2011 Link to this post

       private void OnDropQuery(object sender, DragDropQueryEventArgs e)
            {
                var destination = e.Options.Destination as RadTreeViewItem;
                var destinationtree = e.Options.Destination as RadTreeView;
      
                if (destinationtree != null)
                {
                    object item = (e.Options.Payload as Collection<Object>)[0];
                    if (item.GetType() == typeof(Player))
                    {
                        e.QueryResult = false;
                        return;
                    }
                      
                }
    ......
    Hope this fits in your scenario.

    Hi David,

    In this case the droptarget is the RadTreeView, not the RadtreeViewItem, although destination.dropposition is After. You can do like so:


    Regards,
    Petar Mladenov
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  4. David
    David avatar
    19 posts
    Member since:
    Jun 2008

    Posted 19 Jan 2011 Link to this post

    Hi Petar,

    Thank you. I could hug you right now.
     
    I added what you suggested and made some further changes. Also had to cover a situation where the destination was null.

    Here is the final result, to help out others.

    private void OnDropInsideTreeViewDropQuery(object sender, DragDropQueryEventArgs e)
    {
      var destination = e.Options.Destination as RadTreeViewItem;
      var destinationtree = e.Options.Destination as RadTreeView;
      if (destinationtree != null)
      {
        var item = (e.Options.Payload as Collection<Object>)[0];
        if (item.GetType() == typeof(Player))
        {
          e.QueryResult = false;
          return;
        }
      }
      if (destination != null)
      {
        if (destination.DropPosition == DropPosition.Inside)
        {
          var team = destination.Item as Team;
          if (team != null)
          {
            var item = (e.Options.Payload as Collection<Object>)[0];
            if (item.GetType() == typeof(Team))
            {
              e.QueryResult = false;
            }
          }
        }
        else
        {
          var team = destination.Item as Team;
          if (team != null)
          {
            var item = (e.Options.Payload as Collection<Object>)[0];
            if (item.GetType() == typeof(Player))
            {
              e.QueryResult = false;
            }
          }
          var player= destination.Item as Player;
          if (player != null)
          {
            var item = (e.Options.Payload as Collection<Object>)[0];
            if (item.GetType() == typeof(Team))
            {
              e.QueryResult = false;
            }
          }
        }
      }
    }

    BTW: My mothers madien name is Mladenova, so we may be related.
      

Back to Top