aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.DragAndDrop/DragAndDropService.cs
blob: 320891c611eae495b0854864b231d0a3d8ed7e2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace TestApp
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
    }
}
id='n248' href='#n248'>248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Tango.Core;
using Tango.Core.EventArguments;


namespace Tango.DragAndDrop
{
    /// <summary>
    /// Represents a drag and drop service which exposes several attached properties and events to create an interactive drag and drop functionality.
    /// </summary>
    /// <seealso cref="DraggingSurface" />
    /// <seealso cref="DropEventArgs" />
    public static class DragAndDropService
    {
        private static WeakList<FrameworkElement> _dragElements;
        private static WeakList<FrameworkElement> _dropElements;
        private static Point _mouseDownLocation;
        private static FrameworkElement _currentDragedElement;
        private static FrameworkElement _currentDropElement;
        private static bool _isMouseDown;
        private static DispatcherTimer _dragTimer;
        private static Border _dragBorder;
        private static TouchDevice _touchDevice;
        //private const int MIN_DRAG_OFFSET = 8;

        #region Events

        public static event EventHandler<FrameworkElement> DragStarted;
        public static event EventHandler<FrameworkElement> DragEnded;

        #endregion

        #region Delegates

        /// <summary>
        /// Represents the attached <see cref="DropEvent"/> event delegate.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="DropEventArgs"/> instance containing the event data.</param>
        public delegate void DropEventHandler(object sender, DropEventArgs e);

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes the <see cref="DragAndDropService"/> class.
        /// </summary>
        static DragAndDropService()
        {
            _dragElements = new WeakList<FrameworkElement>();
            _dropElements = new WeakList<FrameworkElement>();
            _dragTimer = new DispatcherTimer();
            _dragTimer.Interval = TimeSpan.FromMilliseconds(30);
            _dragTimer.Tick += DragTimer_Tick;
            _dragTimer.IsEnabled = false;
        }

        #endregion

        #region Attached Properties

        #region MinDragOffset

        /// <summary>
        /// Determines whether an element is MinDragOffset by the drag and drop service.
        /// </summary>
        public static readonly DependencyProperty MinDragOffsetProperty =
            DependencyProperty.RegisterAttached("MinDragOffset",
            typeof(int), typeof(DragAndDropService),
            new FrameworkPropertyMetadata(8));
        /// <summary>
        /// Sets the MinDragOffset attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">if set to <c>true</c> [value].</param>
        public static void SetMinDragOffset(FrameworkElement element, int value)
        {
            element.SetValue(MinDragOffsetProperty, value);
        }

        /// <summary>
        /// Gets the MinDragOffset attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static int GetMinDragOffset(FrameworkElement element)
        {
            return (int)element.GetValue(MinDragOffsetProperty);
        }

        #endregion

        #region DropCommand

        /// <summary>
        /// Determines whether an element is DropCommand by the drag and drop service.
        /// </summary>
        public static readonly DependencyProperty DropCommandProperty =
            DependencyProperty.RegisterAttached("DropCommand",
            typeof(ICommand), typeof(DragAndDropService),
            new FrameworkPropertyMetadata(null));
        /// <summary>
        /// Sets the DropCommand attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">if set to <c>true</c> [value].</param>
        public static void SetDropCommand(FrameworkElement element, ICommand value)
        {
            element.SetValue(DropCommandProperty, value);
        }

        /// <summary>
        /// Gets the DropCommand attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static ICommand GetDropCommand(FrameworkElement element)
        {
            return (ICommand)element.GetValue(DropCommandProperty);
        }

        #endregion

        #region Draggable

        /// <summary>
        /// Determines whether an element is draggable by the drag and drop service.
        /// </summary>
        public static readonly DependencyProperty DraggableProperty =
            DependencyProperty.RegisterAttached("Draggable",
            typeof(bool), typeof(DragAndDropService),
            new FrameworkPropertyMetadata(false, DraggableChanged));

        /// <summary>
        /// Draggable changed.
        /// </summary>
        /// <param name="d">The d.</param>
        /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void DraggableChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                RegisterDraggable(d as FrameworkElement);
            }
            else
            {
                UnRegisterDraggable(d as FrameworkElement);
            }
        }

        /// <summary>
        /// Sets the draggable attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">if set to <c>true</c> [value].</param>
        public static void SetDraggable(FrameworkElement element, bool value)
        {
            element.SetValue(DraggableProperty, value);
        }

        /// <summary>
        /// Gets the draggable attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static bool GetDraggable(FrameworkElement element)
        {
            return (bool)element.GetValue(DraggableProperty);
        }

        #endregion

        #region IsDragging

        /// <summary>
        /// Determines whether the draggable element is currently being dragged.
        /// </summary>
        public static readonly DependencyProperty IsDraggingProperty =
            DependencyProperty.RegisterAttached("IsDragging",
            typeof(bool), typeof(DragAndDropService),
            new FrameworkPropertyMetadata(false));

        /// <summary>
        /// Sets the IsDragging attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">if set to <c>true</c> [value].</param>
        public static void SetIsDragging(FrameworkElement element, bool value)
        {
            element.SetValue(IsDraggingProperty, value);
        }

        /// <summary>
        /// Gets the is dragging attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static bool GetIsDragging(FrameworkElement element)
        {
            return (bool)element.GetValue(IsDraggingProperty);
        }

        #endregion

        #region DraggableBackground

        /// <summary>
        /// Determines the draggable element replication background.
        /// </summary>
        public static readonly DependencyProperty DraggableBackgroundProperty =
            DependencyProperty.RegisterAttached("DraggableBackground",
            typeof(Brush), typeof(DragAndDropService),
            new FrameworkPropertyMetadata(null));

        /// <summary>
        /// Sets the draggable background attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">The value.</param>
        public static void SetDraggableBackground(FrameworkElement element, Brush value)
        {
            element.SetValue(DraggableBackgroundProperty, value);
        }

        /// <summary>
        /// Gets the draggable background attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static Brush GetDraggableBackground(FrameworkElement element)
        {
            return (Brush)element.GetValue(DraggableBackgroundProperty);
        }

        #endregion

        #region DraggableBorderBrush

        /// <summary>
        /// Determines the draggable element replication border brush.
        /// </summary>
        public static readonly DependencyProperty DraggableBorderBrushProperty =
            DependencyProperty.RegisterAttached("DraggableBorderBrush",
            typeof(Brush), typeof(DragAndDropService),
            new FrameworkPropertyMetadata(null));

        /// <summary>
        /// Sets the draggable border brush attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">The value.</param>
        public static void SetDraggableBorderBrush(FrameworkElement element, Brush value)
        {
            element.SetValue(DraggableBorderBrushProperty, value);
        }

        /// <summary>
        /// Gets the draggable border brush attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static Brush GetDraggableBorderBrush(FrameworkElement element)
        {
            return (Brush)element.GetValue(DraggableBorderBrushProperty);
        }

        #endregion

        #region DraggableOpacity

        /// <summary>
        /// Determines the draggable element replication opacity.
        /// </summary>
        public static readonly DependencyProperty DraggableOpacityProperty =
            DependencyProperty.RegisterAttached("DraggableOpacity",
            typeof(double), typeof(DragAndDropService),
            new FrameworkPropertyMetadata(0.8));

        /// <summary>
        /// Sets the draggable opacity attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">The value.</param>
        public static void SetDraggableOpacity(FrameworkElement element, double value)
        {
            element.SetValue(DraggableOpacityProperty, value);
        }

        /// <summary>
        /// Gets the draggable opacity attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static double GetDraggableOpacity(FrameworkElement element)
        {
            return (double)element.GetValue(DraggableOpacityProperty);
        }

        #endregion

        #region Droppable

        /// <summary>
        /// Determines whether an element is a dropable target by the drag and drop service.
        /// </summary>
        public static readonly DependencyProperty DroppableProperty =
            DependencyProperty.RegisterAttached("Droppable",
            typeof(bool), typeof(DragAndDropService),
            new FrameworkPropertyMetadata(false, DroppableChanged));

        /// <summary>
        /// Droppables the changed attached property.
        /// </summary>
        /// <param name="d">The d.</param>
        /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void DroppableChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                RegisterDroppable(d as FrameworkElement);
            }
            else
            {
                UnRegisterDroppable(d as FrameworkElement);
            }
        }

        /// <summary>
        /// Sets the droppable attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">if set to <c>true</c> [value].</param>
        public static void SetDroppable(FrameworkElement element, bool value)
        {
            element.SetValue(DroppableProperty, value);
        }

        /// <summary>
        /// Gets the droppable attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static bool GetDroppable(FrameworkElement element)
        {
            return (bool)element.GetValue(DroppableProperty);
        }

        #endregion

        #region IsDraggableOver

        /// <summary>
        /// Determines whether a draggable element currently hovers over the droppable element.
        /// </summary>
        public static readonly DependencyProperty IsDraggableOverProperty =
            DependencyProperty.RegisterAttached("IsDraggableOver",
            typeof(bool), typeof(DragAndDropService),
            new FrameworkPropertyMetadata(false));

        /// <summary>
        /// Sets the is draggable over attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">if set to <c>true</c> [value].</param>
        public static void SetIsDraggableOver(FrameworkElement element, bool value)
        {
            element.SetValue(IsDraggableOverProperty, value);
        }

        /// <summary>
        /// Gets the is draggable over attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static bool GetIsDraggableOver(FrameworkElement element)
        {
            return (bool)element.GetValue(IsDraggableOverProperty);
        }

        #endregion

        #region Dragging Surface

        /// <summary>
        /// Determines the Draggable dragging surface.
        /// </summary>
        public static readonly DependencyProperty DraggingSurfaceProperty =
            DependencyProperty.RegisterAttached("DraggingSurface",
            typeof(DraggingSurface), typeof(DragAndDropService),
            new FrameworkPropertyMetadata(null, DraggingSurfaceChanged));

        /// <summary>
        /// Dragging the surface changed.
        /// </summary>
        /// <param name="d">The d.</param>
        /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void DraggingSurfaceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }

        /// <summary>
        /// Sets the dragging surface attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">The value.</param>
        public static void SetDraggingSurface(FrameworkElement element, DraggingSurface value)
        {
            element.SetValue(DraggingSurfaceProperty, value);
        }

        /// <summary>
        /// Gets the dragging surface attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static DraggingSurface GetDraggingSurface(FrameworkElement element)
        {
            return (DraggingSurface)element.GetValue(DraggingSurfaceProperty);
        }

        #endregion

        #endregion

        #region Attached Events

        /// <summary>
        /// Occurs when a successful drop has occurred. 
        /// </summary>
        public static readonly RoutedEvent DropEvent =
            EventManager.RegisterRoutedEvent("Drop",
                                            RoutingStrategy.Bubble,
                                            typeof(DropEventHandler),
                                            typeof(DragAndDropService));

        /// <summary>
        /// Adds the drop event handler.
        /// </summary>
        /// <param name="o">The o.</param>
        /// <param name="handler">The handler.</param>
        public static void AddDropHandler(DependencyObject o, DropEventHandler handler)
        {
            ((UIElement)o).AddHandler(DropEvent, handler);
        }

        /// <summary>
        /// Removes the drop event handler.
        /// </summary>
        /// <param name="o">The o.</param>
        /// <param name="handler">The handler.</param>
        public static void RemoveDropHandler(DependencyObject o, DropEventHandler handler)
        {
            ((UIElement)o).RemoveHandler(DropEvent, handler);
        }

        #endregion

        #region Drag Timer

        /// <summary>
        /// Handles the Tick event of the DragTimer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private static void DragTimer_Tick(object sender, EventArgs e)
        {
            if ((Mouse.LeftButton == MouseButtonState.Pressed || (_touchDevice != null && _touchDevice.IsActive)) && _currentDragedElement != null)
            {
                DragStarted?.Invoke(_currentDragedElement, _currentDragedElement);

                var surface = GetDraggingSurface(_currentDragedElement);
                var rootElement = surface.Parent as FrameworkElement;

                if (_touchDevice != null)
                {
                    DraggingSurface.SetLeft(_dragBorder, _touchDevice.GetTouchPoint(surface).Position.X - _mouseDownLocation.X);
                    DraggingSurface.SetTop(_dragBorder, _touchDevice.GetTouchPoint(surface).Position.Y - _mouseDownLocation.Y);
                }
                else
                {
                    DraggingSurface.SetLeft(_dragBorder, Mouse.GetPosition(surface).X - _mouseDownLocation.X);
                    DraggingSurface.SetTop(_dragBorder, Mouse.GetPosition(surface).Y - _mouseDownLocation.Y);
                }

                _dragBorder.Visibility = Visibility.Visible;

                bool found = false;

                foreach (var dropElement in _dropElements.Where(x => x != null && x != _currentDragedElement))
                {
                    if (!dropElement.IsLoaded)
                    {
                        dropElement.UpdateLayout();
                        dropElement.InvalidateArrange();
                        dropElement.InvalidateMeasure();
                        dropElement.InvalidateVisual();
                        continue;
                    }

                    //Point dropPoint = dropElement.TransformToAncestor(rootElement).Transform(new Point(0, 0));
                    Point dropPoint = dropElement.PointToScreen(new Point(0, 0));
                    var dropElementScaledSize = dropElement.PointToScreen(new Point(dropElement.ActualWidth, dropElement.ActualHeight)) - dropElement.PointToScreen(new Point(0, 0));
                    Rect dropRect = new Rect(dropPoint, new Size(dropElementScaledSize.X, dropElementScaledSize.Y));
                    Rect dragRect = new Rect(Canvas.GetLeft(_dragBorder), Canvas.GetTop(_dragBorder), _dragBorder.Width / 2, _dragBorder.Height);

                    Point mousePoint = new Point();

                    if (_touchDevice != null)
                    {
                        mousePoint = rootElement.PointToScreen(_touchDevice.GetTouchPoint(rootElement).Position);
                    }
                    else
                    {
                        mousePoint = rootElement.PointToScreen(Mouse.GetPosition(rootElement));
                    }

                    if (dropRect.IntersectsWith(new Rect(mousePoint, new Size(1, 1))))
                    {
                        SetIsDraggableOver(dropElement, true);

                        foreach (var drop in _dropElements.Where(x => x != null && x != dropElement))
                        {
                            SetIsDraggableOver(drop, false);
                        }

                        _currentDropElement = dropElement;
                        found = true;
                    }
                    else
                    {
                        SetIsDraggableOver(dropElement, false);
                    }
                }

                if (!found)
                {
                    _currentDropElement = null;
                }
            }
            else
            {
                DragEnded?.Invoke(_currentDragedElement, _currentDragedElement);
                DropDraggable();
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Registers a framework element as a draggable element.
        /// </summary>
        /// <param name="element">The element.</param>
        private static void RegisterDraggable(FrameworkElement element)
        {
            foreach (var item in _dragElements.ToList())
            {
                if (item == null)
                {
                    _dragElements.Remove(item);
                }
            }

            if (!_dragElements.Contains(element))
            {
                _dragElements.Add(element);

                element.RegisterForPreviewMouseOrTouchDown(Draggable_PreviewMouseDown);
                element.RegisterForMouseOrTouchMove(Draggable_MouseMove);
                //element.PreviewMouseUp += Draggable_PreviewMouseUp;
                element.AddHandler(FrameworkElement.PreviewMouseUpEvent, new MouseButtonEventHandler(Draggable_PreviewMouseUp));
                //element.IsManipulationEnabled = true;

                //element.AddHandler(FrameworkElement.PreviewMouseDownEvent, (MouseButtonEventHandler)Draggable_PreviewMouseDown, true);
                //element.AddHandler(FrameworkElement.MouseMoveEvent, (MouseEventHandler)Draggable_MouseMove, true);
                //element.AddHandler(FrameworkElement.PreviewMouseUpEvent, (MouseButtonEventHandler)Draggable_PreviewMouseUp, true);
                //element.Unloaded += Draggable_Unloaded;
            }
        }

        /// <summary>
        /// Unregisters a framework element as a draggable element.
        /// </summary>
        /// <param name="element">The element.</param>
        private static void RegisterDroppable(FrameworkElement element)
        {
            foreach (var item in _dropElements.ToList())
            {
                if (item == null)
                {
                    _dropElements.Remove(item);
                }
            }

            if (!_dropElements.Contains(element))
            {
                _dropElements.Add(element);
                //element.Unloaded += Droppable_Unloaded;
            }
        }

        /// <summary>
        /// Handles the Unloaded event of the Element control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private static void Draggable_Unloaded(object sender, RoutedEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            UnRegisterDraggable(element);
        }

        /// <summary>
        /// Handles the Unloaded1 event of the Element control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private static void Droppable_Unloaded(object sender, RoutedEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            UnRegisterDroppable(element);
        }

        /// <summary>
        /// Registers a framework element as a droppable element.
        /// </summary>
        /// <param name="element">The element.</param>
        private static void UnRegisterDraggable(FrameworkElement element)
        {
            _dragElements.Remove(element);
            element.PreviewMouseUp -= Draggable_PreviewMouseUp;
            element.Unloaded -= Draggable_Unloaded;
        }

        /// <summary>
        /// Unregisters a framework element as a droppable element.
        /// </summary>
        /// <param name="element">The element.</param>
        private static void UnRegisterDroppable(FrameworkElement element)
        {
            _dropElements.Remove(element);
            element.Unloaded -= Droppable_Unloaded;
        }

        /// <summary>
        /// Creates the dragging element replication.
        /// </summary>
        private static void CreateDraggingElement()
        {
            if (_currentDragedElement != null)
            {
                BitmapSource source = _currentDragedElement.TakeSnapshot();
                _dragBorder = new Border();

                var surface = GetDraggingSurface(_currentDragedElement);

                _dragBorder.Background = GetDraggableBackground(_currentDragedElement);
                _dragBorder.BorderThickness = new Thickness(1);
                _dragBorder.BorderBrush = GetDraggableBorderBrush(_currentDragedElement);
                //dragBorder.CornerRadius = new CornerRadius(currentDragedElement.BorderRadius);

                _dragBorder.Visibility = Visibility.Hidden;
                _dragBorder.IsHitTestVisible = false;
                _dragBorder.Width = source.PixelWidth;
                _dragBorder.Height = source.PixelHeight;
                _dragBorder.Opacity = GetDraggableOpacity(_currentDragedElement);

                Image img = new Image();
                _dragBorder.Child = img;
                img.Stretch = System.Windows.Media.Stretch.Fill;
                img.Source = source;

                ScaleTransform scale = new ScaleTransform(1, 1, 0.5, 0.5);
                _dragBorder.RenderTransformOrigin = new Point(0.5, 0.5);
                _dragBorder.RenderTransform = scale;
                surface.Children.Add(_dragBorder);

                DoubleAnimation ani = new DoubleAnimation(0.95, new Duration(TimeSpan.FromMilliseconds(300)));
                ani.AutoReverse = true;
                ani.RepeatBehavior = RepeatBehavior.Forever;

                DoubleAnimation aniOp = new DoubleAnimation(_dragBorder.Opacity, 0.5, new Duration(TimeSpan.FromMilliseconds(300)));
                aniOp.AutoReverse = true;
                aniOp.RepeatBehavior = RepeatBehavior.Forever;

                scale.BeginAnimation(ScaleTransform.ScaleXProperty, ani);
                scale.BeginAnimation(ScaleTransform.ScaleYProperty, ani);
                _dragBorder.BeginAnimation(ContentControl.OpacityProperty, aniOp);
            }
        }

        /// <summary>
        /// Removes the dragging element replication.
        /// </summary>
        private static void RemoveDraggingElement()
        {
            if (_dragBorder != null)
            {
                _dragBorder.RenderTransform.BeginAnimation(ScaleTransform.ScaleXProperty, null);
                _dragBorder.RenderTransform.BeginAnimation(ScaleTransform.ScaleYProperty, null);
                _dragBorder.BeginAnimation(ContentControl.OpacityProperty, null);
            }

            var surface = GetDraggingSurface(_currentDragedElement);
            surface.Children.Clear();
        }

        /// <summary>
        /// Drops the draggable.
        /// </summary>
        /// <param name="noDrop">if set to <c>true</c> [no drop].</param>
        private static void DropDraggable(bool noDrop = false)
        {
            _isMouseDown = false;
            RemoveDraggingElement();
            _dragTimer.Stop();
            SetIsDragging(_currentDragedElement, false);

            foreach (var dropElement in _dropElements.Where(x => x != null))
            {
                SetIsDraggableOver(dropElement, false);
            }

            if (noDrop)
            {
                _currentDragedElement = null;
                _currentDropElement = null;
                return;
            }

            if (_currentDropElement != null && _currentDragedElement != null)
            {
                _currentDropElement.RaiseEvent(new DropEventArgs(DropEvent, _currentDragedElement, _currentDropElement, _mouseDownLocation));

                var command = GetDropCommand(_currentDropElement);
                if (command != null)
                {
                    command.Execute(new DropEventArgs(DropEvent, _currentDragedElement, _currentDropElement, _mouseDownLocation));
                }

                _currentDragedElement = null;
                _currentDropElement = null;
            }
            else if (_currentDropElement == null && _currentDragedElement != null)
            {
                //if (DroppedNoWhere != null) DroppedNoWhere(this, currentDragedElement.Element);
                //TODO: ??
            }
        }

        #endregion

        #region Draggable Event Handlers

        private static void Draggable_PreviewMouseDown(object sender, MouseOrTouchEventArgs e)
        {
            var dragThumb = (sender as FrameworkElement).FindChild<DragThumb>();

            if ((e.Source != sender && dragThumb == null) || (dragThumb != null && dragThumb != e.OriginalSource))
            {
                _currentDragedElement = null;
                return;
            }

            FrameworkElement element = sender as FrameworkElement;

            if (!_dragElements.Where(x => x != null).ToList().Exists(x => x == element))
            {
                e.Handled = false;
                return;
            }

            _mouseDownLocation = e.GetPosition(element);
            _currentDragedElement = element;
            _isMouseDown = true;
        }



        /// <summary>
        /// Handles the MouseMove event of the Draggable control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param>
        private static void Draggable_MouseMove(object sender, MouseOrTouchEventArgs e)
        {
            if (_isMouseDown)
            {
                FrameworkElement element = _currentDragedElement;

                if (element != null)
                {
                    int minDragOffset = GetMinDragOffset(element);

                    var surface = GetDraggingSurface(element);

                    if (surface != null)
                    {
                        if ((e.GetPosition(element).X > _mouseDownLocation.X + minDragOffset || e.GetPosition(element).X < _mouseDownLocation.X - minDragOffset) || (e.GetPosition(element).Y > _mouseDownLocation.Y + minDragOffset || e.GetPosition(element).Y < _mouseDownLocation.Y - minDragOffset))
                        {
                            element.ReleaseMouseCapture();
                            surface.ReleaseMouseCapture();

                            if (e.TouchDevice != null)
                            {
                                element.ReleaseTouchCapture(e.TouchDevice);
                                surface.ReleaseTouchCapture(e.TouchDevice);
                            }

                            _isMouseDown = false;
                            CreateDraggingElement();
                            _touchDevice = e.TouchDevice;
                            _dragTimer.Start();
                            SetIsDragging(element, true);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Handles the PreviewMouseUp event of the Draggable control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        private static void Draggable_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            FrameworkElement element = _currentDragedElement;

            if (element != null)
            {
                e.Handled = true;

                if ((e.GetPosition(element).X > _mouseDownLocation.X + 10 || e.GetPosition(element).X < _mouseDownLocation.X - 10) || (e.GetPosition(element).Y > _mouseDownLocation.Y + 10 || e.GetPosition(element).Y < _mouseDownLocation.Y - 10))
                {
                    DropDraggable();
                }
                else
                {
                    DropDraggable(true);
                }
            }
        }

        #endregion
    }
}