コード例 #1
0
        private void AtDrop(object sender, DragEventArgs e)
        {
            //if the data type can be dropped
            if (_dataType != null)
            {
                if (e.Data.GetDataPresent(_dataType))
                {
                    //first find the UIElement that it was dropped over, then we determine if it's
                    //dropped above or under the UIElement, then insert at the correct index.
                    var dropContainer = (ItemsControl)sender;
                    //get the UIElement that was dropped over
                    var droppedOverItem = UIHelper.GetUIElement(dropContainer, e.GetPosition(dropContainer));
                    int dropIndex       = -1;               //the location where the item will be dropped
                    dropIndex = dropContainer.ItemContainerGenerator.IndexFromContainer(droppedOverItem) + 1;
                    //find if it was dropped above or below the index item so that we can insert
                    //the item in the correct place
                    if (UIHelper.IsPositionAboveElement(droppedOverItem, e.GetPosition(droppedOverItem))) //if above
                    {
                        dropIndex = dropIndex - 1;                                                        //we insert at the index above it
                    }
                    //remove the data from the source
                    var source = (IDragable)e.Data.GetData(_dataType);
                    source.Remove(e.Data.GetData(_dataType));

                    //drop the data
                    var target = (IDropable)AssociatedObject.DataContext;
                    target.Drop(e.Data.GetData(_dataType), dropIndex);
                }
            }
            if (_insertAdornerManager != null)
            {
                _insertAdornerManager.Clear();
            }
            e.Handled = true;
        }