Example #1
0
        }        // ReSharper restore UnusedMember.Global

        private static void SourceChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            DependencyObject Element       = sender as DependencyObject;
            IDragDropSource  NewSource     = e.NewValue as IDragDropSource;
            IDragDropSource  CurrentSource = e.OldValue as IDragDropSource;

            if (Element is UIElement || Element is ContentElement)
            {
                if (CurrentSource != null)
                {
                    DragDropSourceAdapter Adapter;
                    DragDrop.dragDropSources.TryGetValue(Element, out Adapter).DbC_Assure(isSuccess => isSuccess);
                    DragDrop.dragDropSources.Remove(Element);
                    Adapter?.Dispose();
                }

                if (NewSource != null)
                {
                    DragDropSourceAdapter Adapter = DragDropSourceAdapter.Create(NewSource, Element);
                    DragDrop.dragDropSources.Add(Element, Adapter);
                }
            }
            else
            {
                throw new InvalidOperationException("Drag and Drop source can only be registered on UIElements and ContentElements");
            }
        }
        public void DoDragDrop()
        {
            object        DragData      = this.sourceHandler.DragData;
            TypeConverter TypeConverter = TypeDescriptor.GetConverter(DragData.GetType());

            IDataObject DataObject = null;

            if (TypeConverter != null)
            {
                if (TypeConverter.CanConvertTo(typeof(IDataObject)))
                {
                    DataObject = (IDataObject)TypeConverter.ConvertTo(DragData, typeof(IDataObject));
                }
            }

            if (DataObject == null)
            {
                if (DragData.GetType().GetCustomAttributes(typeof(SerializableAttribute), true).Length > 0)
                {
                    DataObject = new DataObject(DragData);
                }
                else
                {
                    DataObject = new DataObject(new DragDropObjectWrapper(new DataObject(DragData)));

                    /*throw new InvalidOperationException(
                     *  string.Format(
                     *      "The object type '{0}' that shall be dragged & dropped is not serialiasable. You have to mark it with the [Serializable] attribute. Non-serializable objects cannot be moved outside the window border. If you don't want or can't provide real serializability,or don't want to have the item available outside the application anyway, add the ISerializable interface with an empty implementation",
                     *      DragData.GetType()));*/
                }
            }

            DragDropEffects DropEffect = System.Windows.DragDrop.DoDragDrop(this.source, DataObject, this.sourceHandler.DragEffects);

            this.sourceHandler.NotifyDropped(DragDropSourceAdapter.ToDragDropEffect(DropEffect));
        }