drag treeview item to another control

6 posts, 0 answers
  1. Marc
    Marc avatar
    1 posts
    Member since:
    Mar 2009

    Posted 26 Mar 2009 Link to this post

    Hello!

    I am willing to buy your wpf controls, but before, I need to be sure that I do following stuff with it:

    1. drag items to other controls than the treeview itself (for that case, if would be nice if I still would have the "drag tooltip")
    2. being able to change the texts "drop in", "drop before", etc...
    3. Do filtering (I explain here what I need):
    I have the following structure: 
    aaa
       bbb
       ccc
          fff
    ddd
       eee 
    I enter a filter criteria "fff". Will the node be displayed, even though the node "aaa" and "ccc" do not match the criteria?

    Thanks for your quick answer.

    Best regards,

    Marc Wuergler
  2. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 31 Mar 2009 Link to this post

    Hello Marc,

    First, sorry for the delayed reply.

    Straight to your questions:

    1. The built-in DragDrop only works within the TreeView and you cannot extend it to other controls. Yet, you can use the regular DragDrop in WPF which is a bit more complex and more powerful (it supports dragging across applications but you need more code and care to get it going). In short, you can drag items to other controls but you will not be able to use it with the drag tooltips.

    2. Yes, you can change the text of the drag tooltips by just setting properties on the RadTreeView.

    3. It took me a while to research all the options for this kind of filtering, but here are my suggestions:

    a) Bind the TreeView to am observable collection (ViewModel) and filter this collection. This approach is more testable but requires filtering to be built in the view-model, which may not be applicable in all cases.

    b) Use the built in filtering mechanism in the Items CollectionView. For example you can apply a filter to all the items collection of the tree view. This is the so-called view filtering, when the ItemsSource remains unchanged.:

    public partial class Window1 : Window  
    {  
        Predicate<object> myFilter;  
     
        public Window1()  
        {  
            InitializeComponent();  
     
     
            //TODO: Set the ItemsSource here.  
     
            myFilter = new Predicate<object>((item) =>  
                {  
                    var myItem = item as MyData;  
                    return myItem.Name.Contains("Pr") || myItem.GetDescendants().Any(desc => desc.Name.Contains("Pr"));  
                });  
     
            treeView.Items.Filter = myFilter;  
     
            treeView.ItemPrepared += new EventHandler<Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs>(treeView_ItemPrepared);  
        }  
     
        void treeView_ItemPrepared(object sender, Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs e)  
        {  
            var item = e.PreparedItem;  
            item.Items.Filter = null;  
            item.Items.Filter = myFilter;  
            item.IsExpanded = true;  
        }  
     
        private void filterBox_TextChanged(object sender, TextChangedEventArgs e)  
        {  
            myFilter = new Predicate<object>((item) =>  
            {  
                var myItem = item as MyData;  
                return myItem.Name.Contains(filterBox.Text) || myItem.GetDescendants().Any(desc => desc.Name.Contains(filterBox.Text));  
            });  
     
            if (treeView != null)  
            {  
                treeView.Items.Filter = myFilter;  
            }  
        }  
    }  
     

    For this approach you need to be able to enumerate the descendants of you DataItems.

    c) Enumerate the TreeViewItems and set the visibility according to a similar rule.

    d) Bind the visibility property of the TreeViewItem to the filter TextBox (or other filter source) with a converter that will show/hide it based on the criteria. This is almost the same as the above, just you do not need to enumerate all the items explicitly.

    The easiest to implement is d) while the best might be a) since it is most testable.

    Please feel free to come back to us with more questions or suggestions,

    Sincerely yours,
    Miroslav
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Lauren Nickerson
    Lauren Nickerson avatar
    52 posts
    Member since:
    Jul 2009

    Posted 22 Dec 2009 Link to this post

    Hi, so what I understand from this answer is that I can't have both? Say, use regular WPF D&D to drop into another control, and use your D&D to drop within the treeview? Or are they incompatible?

    The thing is that, again, based on your answer; I think I need both. In our application we want to show the tooltip that show the name of the RadTreeViewItem we're dragging, but if we move to WPF D&D we would lose that right?

    Unless there's a way to create our on tooltip template to still make it look like yours, and use WPF D&D? What other options do we have?

    Thanks.
  5. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 22 Dec 2009 Link to this post

    Hello Lauren Nickerson,

    My reply from the end of march is not up-to-date anymore.

    Now the Telerik DragDrop supports dragging across controls. DragDrop between TreeViews is supported out-of-the-box, there are examples with ListBoxes and the GridView. If we do not have an example with a particular control, we will be happy to help you with it. You find drag-drop examples in the online demos:

    http://demos.telerik.com/wpf/

    check the DragDrop and TreeView examples.

    Unfortunately DragDrop across different windows and applications is not possible with the Telerik DragDrop yet. If you would like to support this scenario then you will need to use the built-in WPF DragDrop. Do you need this in your application?

    All the best,
    Miroslav
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  6. Lauren Nickerson
    Lauren Nickerson avatar
    52 posts
    Member since:
    Jul 2009

    Posted 22 Dec 2009 Link to this post

    EDIT:

    Hi, I was able to overcome all the previous problems, but I have one remaining one. When I drag over my custom control, I get the "don't" sign in the cursor, however, when I drop, it works. Here's my code on the OnDropQuery event for my custom control:

            private void OnDropQuery(object sender, DragDropQueryEventArgs e) 
            { 
                bool isDragItem = (e.Options.Payload as IList)[0] is DragItem; 
                bool isDropItem = this.Item is DropItem; 
     
                if (! isDragItem  || !isDropItem) 
                { 
                    e.QueryResult = false
                } 
                else 
                { 
                    e.QueryResult = true
                } 
            } 

    It's not my exact code but it's analogous to it. I know the e.QueryResult is set to true when I'm dragging over said control, but that doesn't show the regular cursor, it show's the "don't" sign. How can I fix this? I attached a screenshot that shows the problem.

    Thanks!
  7. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 23 Dec 2009 Link to this post

    Hello Lauren Nickerson,

    The TreeView assigns a DragCue, this is an instance of the TreeViewDragCue class. You can set its IsDropPossible property like so:

    var dragCue = e.Options.DragCue as TreeViewDragCue;
    dragCue.IsDropPossible = true;

    Please note that you can replace the drag cue with anything you need if you do not want the default look.

    Best wishes,
    Miroslav
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top
UI for WPF is Visual Studio 2017 Ready