tags:

views:

3531

answers:

1

Hi All, im trying to drag and drop files in my treeview but i have no idea why its breaking down if i run it and try dragind a file.

i'm basically a newby in wpf and the code below is as far i can try please somebody help me on this.

    private void TreeViewItem_Drop( object sender, DragEventArgs e)
    {
        TreeViewItem treeViewItem =
            e.Source as TreeViewItem;
        TreeViewItem obj =
            e.Data.GetData(typeof(TreeViewItem)) as TreeViewItem;

        if ((obj.Parent as TreeViewItem) != null)
        {
            (obj.Parent as TreeViewItem).Items.Remove(obj);
        }
        else
        {
            treeViewItem.Items.Remove(obj);
            treeViewItem.Items.Insert(0, obj);
            e.Handled = true;
        }
    }

    private void TreeViewItem_MouseLeftButtonDown( object sender,MouseButtonEventArgs e)
    {
        DependencyObject dependencyObject =
            _treeview.InputHitTest(e.GetPosition(_treeview))
            as DependencyObject;

        Debug.Write(e.Source.GetType().ToString());

        if (dependencyObject is TextBlock)
        {
            TreeViewItem treeviewItem = e.Source
                as TreeViewItem;

            DragDrop.DoDragDrop(_treeview, _treeview.SelectedValue,
                DragDropEffects.Move);
            e.Handled = true;
        }
    }

Thanx in advance

Austin

+6  A: 

This article is very helpful. Drag drop wpf

This code may be of use to you as well.

Point _startPoint;
bool _IsDragging = false;

void TemplateTreeView_PreviewMouseMove(object sender, MouseEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed || 
        e.RightButton == MouseButtonState.Pressed && !_IsDragging)
    {
        Point position = e.GetPosition(null); 
        if (Math.Abs(position.X - _startPoint.X) > 
                SystemParameters.MinimumHorizontalDragDistance ||
            Math.Abs(position.Y - _startPoint.Y) > 
                SystemParameters.MinimumVerticalDragDistance)
        {
            StartDrag(e);
        }
    }           
}

void TemplateTreeView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    _startPoint = e.GetPosition(null);
}

private void StartDrag(MouseEventArgs e)
{
    _IsDragging = true;
    object temp = this.TemplateTreeView.SelectedItem;
    DataObject data = null;

    data = new DataObject("inadt", temp);

    if (data != null)
    {
        DragDropEffects dde = DragDropEffects.Move;
        if (e.RightButton == MouseButtonState.Pressed)
        {
            dde = DragDropEffects.All;
        }
        DragDropEffects de = DragDrop.DoDragDrop(this.TemplateTreeView, data, dde);
    }
    _IsDragging = false;
}
Erin
Thank Erin the code did help realize what went wrong. In my MousePreviewDown was couple of things wrong, like the point where i didnt use Math.Abs(). Sorry the feedback came this late :)
don
Did that solve the problem then? (Should the question be marked as answered??)
Brett Rigby
it solved the problem for me
don
Mark it as the answer if you would please then.
Erin