private void treeView_DragOver(object sender, DragEventArgs e)
        {
            try
            {
                Point currentPosition = e.GetPosition(mainBuilder);


                if ((Math.Abs(currentPosition.X - _lastMouseDown.X) > 10.0) ||
                    (Math.Abs(currentPosition.Y - _lastMouseDown.Y) > 10.0))
                {
                    // Verify that this is a valid drop and then store the drop target
                    builderItem item = GetNearestContainer(e.OriginalSource as UIElement);
                    if (CheckDropTarget(draggedItem, item))
                    {
                        e.Effects = DragDropEffects.Move;
                    }
                    else
                    {
                        e.Effects = DragDropEffects.None;
                    }
                }
                e.Handled = true;
            }
            catch (Exception)
            {
            }
        }
        private bool CheckDropTarget(builderItem _sourceItem, builderItem _targetItem)
        {
            //Check whether the target item is meeting your condition
            bool _isEqual = false;

            if (!_sourceItem.Header.ToString().Equals(_targetItem.Header.ToString()))
            {
                _isEqual = true;
            }
            return(_isEqual);
        }
        private builderItem GetNearestContainer(UIElement element)
        {
            // Walk up the element tree to the nearest tree view item.
            builderItem container = element as builderItem;

            while ((container == null) && (element != null))
            {
                element   = VisualTreeHelper.GetParent(element) as UIElement;
                container = element as builderItem;
            }
            return(container);
        }
 private void mainBuilder_Drop(object sender, DragEventArgs e)
 {
     if (e.Data.GetDataPresent("myFormat"))
     {
         Point             p         = e.GetPosition(mainBuilder);
         Core.ToolBox.Tool tool      = e.Data.GetData("myFormat") as Core.ToolBox.Tool;
         TreeView          treeView  = sender as TreeView;
         builderItem       itemToAdd = new builderItem();
         itemToAdd.builderTool = tool;
         itemToAdd.Header      = tool.toolHeader;
         treeView.Items.Add(itemToAdd);
     }
 }
        public void addChild(builderItem _sourceItem, builderItem _targetItem)
        {
            // add item in target customTreeViewItem
            builderItem item1 = new builderItem();

            item1.Header      = _sourceItem.Header;
            item1.builderTool = _sourceItem.builderTool;
            _targetItem.Items.Add(item1);
            foreach (builderItem item in _sourceItem.Items)
            {
                addChild(item, item1);
            }
        }
        private void treeView_Drop(object sender, DragEventArgs e)
        {
            try
            {
                e.Effects = DragDropEffects.None;
                e.Handled = true;

                // Verify that this is a valid drop and then store the drop target
                builderItem TargetItem = GetNearestContainer(e.OriginalSource as UIElement);
                if (TargetItem != null && draggedItem != null)
                {
                    _target   = TargetItem;
                    e.Effects = DragDropEffects.Move;
                }
            }
            catch (Exception)
            {
            }
        }
        private void CopyItem(builderItem _sourceItem, builderItem _targetItem)
        {
            try
            {
                //adding dragged customTreeViewItem in target customTreeViewItem
                addChild(_sourceItem, _targetItem);

                //finding Parent customTreeViewItem of dragged customTreeViewItem
                builderItem ParentItem = FindVisualParent <builderItem>(_sourceItem);
                // if parent is null then remove from TreeView else remove from Parent customTreeViewItem
                if (ParentItem == null)
                {
                    mainBuilder.Items.Remove(_sourceItem);
                }
                else
                {
                    ParentItem.Items.Remove(_sourceItem);
                }
            }
            catch
            {
            }
            //}
        }
        private void treeView_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    Point currentPosition = e.GetPosition(mainBuilder);


                    if ((Math.Abs(currentPosition.X - _lastMouseDown.X) > 10.0) ||
                        (Math.Abs(currentPosition.Y - _lastMouseDown.Y) > 10.0))
                    {
                        draggedItem = (builderItem)mainBuilder.SelectedItem;
                        if (draggedItem != null)
                        {
                            DragDropEffects finalDropEffect = DragDrop.DoDragDrop(mainBuilder, mainBuilder.SelectedValue,
                                                                                  DragDropEffects.Move);
                            //Checking target is not null and item is dragging(moving)
                            if ((finalDropEffect == DragDropEffects.Move) && (_target != null))
                            {
                                // A Move drop was accepted
                                if (!draggedItem.Header.ToString().Equals(_target.Header.ToString()))
                                {
                                    CopyItem(draggedItem, _target);
                                    _target     = null;
                                    draggedItem = null;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }