I have treeView issue

Page 1 of 1

8 Replies - 5890 Views - Last Post: 03 October 2011 - 01:14 AM Rate Topic: -----

#1 falcon eyes  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 53
  • Joined: 16-April 07

I have treeView issue

Posted 19 September 2011 - 01:07 PM

Hi every body
I have wrote a reading book organizer applications, it store my favorite books and store some information like sn,book titl,etc ..
and these books are organized in a groups in a TreeView control,the application work fine,but i want my application support
drag and drop in the TreeView, and i began in coding but
i encounter a problem that i can't solve yet,and its
when i drag a book from a folder or from the TreeView its self and move over the TreeView i can't make the TreeView auto select items as i move over it to determine my target for dropping
if any one help i will be appreciate

This post has been edited by falcon eyes: 19 September 2011 - 01:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: I have treeView issue

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: I have treeView issue

Posted 19 September 2011 - 01:22 PM

So where is the code that you have where you have tried?
Was This Post Helpful? 0
  • +
  • -

#3 falcon eyes  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 53
  • Joined: 16-April 07

Re: I have treeView issue

Posted 20 September 2011 - 12:10 PM

well,
this is the markup for the treeview control
<TreeView x:Name="mainTreeVu" 
SelectedItemChanged="mainTreeVu_SelectedItemChanged"  
MouseDoubleClick="mainTreeVu_MouseDoubleClick" 
DragEnter="mainTreeVu_DragEnter" 
Drop="mainTreeVu_Drop" 
AllowDrop="true" 
PreviewMouseDown="mainTreeVu_PreviewMouseDown" 
PreviewMouseMove="mainTreeVu_PreviewMouseMove"
PreviewMouseLeftButtonUp="mainTreeVu_
PreviewMouseLeftButtonUp" 
VirtualizingStackPanel.IsVirtualizing="True" 
VirtualizingStackPanel.VirtualizationMode="Recycling" 
Margin="4,4,4,0" 
Grid.RowSpan="3" 
Height="1835" 
VerticalAlignment="Top" >


this the code for drag enter event
  private void mainTreeVu_DragEnter(object sender, DragEventArgs e)
        {
           
          
            if (e.Data.GetDataPresent( DataFormats.FileDrop,false))
              e.Effects=DragDropEffects.Copy;
            
            if (e.Data.GetDataPresent(typeof(TreeViewItem)))

                e.Effects = DragDropEffects.Move;
        }




and this for Drop Event
 private void mainTreeVu_Drop(object sender, DragEventArgs e)
        {
           //if the dragged item is a file
            if (e.Data.GetDataPresent( DataFormats.FileDrop,false))
            {
                string[] tmpBPathes = (string[])e.Data.GetData(DataFormats.FileDrop);
            if (currentSelectedTreeVUItem != null && currentselectedBook == null)
            {
                foreach (string bp in tmpBPathes)
                {
                    currentSelectedTreeVUItem.Foreground = Brushes.Red;
                    AddBook(bp);
                }
            }
            else
                MessageBox.Show("Select An item then drop on him");
        }
            //if the dragged item is a TreeViewItem
            if (e.Data.GetDataPresent(typeof(TreeViewItem)))
            {
              
                TreeViewItem obj = e.Data.GetData(typeof(TreeViewItem)) as TreeViewItem;
                if(obj!=null)
                {
                    if ((obj.Parent as TreeViewItem) != null)
                        {
                            (obj.Parent as TreeViewItem).Items.Remove(obj);                                      
                             //add the dragged item to the last category in the treeview
                                ((TreeViewItem)mainTreeVu.Items[mainTreeVu.Items.Count - 1]).Items.Add(obj);
                                 e.Handled = true;
                        }
                }
                       // ((TreeViewItem)mainTreeVu.Items[mainTreeVu.Items.Count-1]).Items.Add((TreeViewItem)e.Data.GetData(typeof(TreeViewItem)));
            }
        
        }



this for Mouse down event
  private void mainTreeVu_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            startPoint = e.GetPosition(null);
            lmbDown = true;
        }



this for mouse up event
 private void mainTreeVu_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            lmbDown = false;
        }



but in private void mainTreeVu_PreviewMouseMove(object sender, MouseEventArgs e)
i didn't know what to write.
Was This Post Helpful? 0
  • +
  • -

#4 falcon eyes  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 53
  • Joined: 16-April 07

Re: I have treeView issue

Posted 21 September 2011 - 03:24 PM

Hey any body there
eclipsed4utoo u asked for the code,and the code is above,where's your reply
Was This Post Helpful? -2
  • +
  • -

#5 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: I have treeView issue

Posted 22 September 2011 - 04:20 AM

View Postfalcon eyes, on 21 September 2011 - 11:24 PM, said:

Hey any body there
eclipsed4utoo u asked for the code,and the code is above,where's your reply

Woah there, hold up. Whats with the attitude? You have been posting long enough to realise that D.I.C is run by volunteers who are willing to help others for no personal gain of their own. You can't demand answers to your coding problems. I would be supprised if eclipsed4utoo even dignifies a response like that with an answer.

Now, to answer your question. Try using the class VisualTreeHelper, it helps you navigate the visual tree for DependencyObjects and perform hit testing. IN the following code I hit test on the preview left button up, then step up the tree until I find the treeview item that was hit (if one was). Then I set the background for the item. It doesn't matter what I do to the item, the key point is you can use this method to locate which item was hit. This is probably not the greatest method in the world, but it will get what you want to get done.
private void TreeView_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    HitTestResult r = VisualTreeHelper.HitTest(this.tV, e.GetPosition(this.tV));

    DependencyObject obj = r.VisualHit;

    while (obj != null)
    {
        obj = VisualTreeHelper.GetParent(obj);

        if (obj is TreeViewItem)
        {
            break;
        }
    }

    if (obj is TreeViewItem)
    {
        ((TreeViewItem)obj).Background = Brushes.Red;
    }
}


In the future please try to be a bit more courtious to people who are willing to help you or you may find you get no help at all...

This post has been edited by ragingben: 22 September 2011 - 06:48 AM

Was This Post Helpful? 1
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: I have treeView issue

Posted 22 September 2011 - 06:30 AM

View Postfalcon eyes, on 21 September 2011 - 06:24 PM, said:

Hey any body there
eclipsed4utoo u asked for the code,and the code is above,where's your reply


It's called having a full-time job and a life that doesn't allow me to sit on this forum waiting for you to post code that you should have posted in the original post.
Was This Post Helpful? 0
  • +
  • -

#7 falcon eyes  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 53
  • Joined: 16-April 07

Re: I have treeView issue

Posted 02 October 2011 - 01:25 PM

Sorry ,
i didn't meant to make any body angry from me i just was Rushing the answer,and thanks for the answer
and thanks for all your efforts
Was This Post Helpful? 0
  • +
  • -

#8 falcon eyes  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 53
  • Joined: 16-April 07

Re: I have treeView issue

Posted 02 October 2011 - 03:30 PM

thanks ragingben
i have modified your code to fit my application and it works fine thanks for help,and below is the modified code

 private void mainTreeVu_PreviewMouseMove(object sender, MouseEventArgs e)
        {
              //this code for auto select the treview item while dragging an item over it
            if (_isDragging )
          
            {
                HitTestResult r = VisualTreeHelper.HitTest(this.mainTreeVu, e.GetPosition(this.mainTreeVu));

                DependencyObject obj = r.VisualHit;

                while (obj != null)
                {
                    obj = VisualTreeHelper.GetParent(obj);

                   if (obj is TreeViewItem)
                    {
                      
                        break;
                    }
                }

                if (obj is TreeViewItem)
                {  //auto highlight the item
                    ((TreeViewItem)obj).IsSelected = true;
                    //set the current treeviewitem which will receive the dropped item
                    currentSelectedTreeVUItem = ((TreeViewItem)obj);
                    //if the treeview item is not expanded then expand it
                   if( (!((TreeViewItem)obj).IsExpanded))
                       ((TreeViewItem)obj).IsExpanded=true;

                                    
                 }
            }



thanks for all of you
Was This Post Helpful? 0
  • +
  • -

#9 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: I have treeView issue

Posted 03 October 2011 - 01:14 AM

Excellent, I'm glad I could help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1