This is a migrated thread and some comments may be shown as answers.

Only allowing drop where it is appropriate within the hierarachy

3 Answers 189 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 17 Jan 2011, 02:12 PM
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?

3 Answers, 1 is accepted

Sort by
0
Shawn
Top achievements
Rank 1
answered on 18 Jan 2011, 11:32 PM
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
0
Accepted
Petar Mladenov
Telerik team
answered on 19 Jan 2011, 06:27 PM
   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>>
0
David
Top achievements
Rank 1
answered on 20 Jan 2011, 12:10 AM

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.
  

Tags
TreeView
Asked by
David
Top achievements
Rank 1
Answers by
Shawn
Top achievements
Rank 1
Petar Mladenov
Telerik team
David
Top achievements
Rank 1
Share this question
or