예제 #1
0
        private void OnGridViewDragOver(object sender, DragEventArgs e)
        {
            if (this.draggedItems.Count == 0)
            {
                return;
            }

            e.AcceptedOperation = DataPackageOperation.Move;

            // lot of hacks in this method...
            Point intersectingPoint = e.GetPosition(this.mainPage);

            // first is to find a GridViewItem based on the current mouse position
            FrameworkElement element = null;
            int index = 0;

            while (element == null && index < 2)
            {
                // use X from touch event, but find out Y because user can drag of an empty
                // area (if he moves the first item of a column)

                // get the one of the first GridViewItem in the list just to get its height + Y position
                var container = this.gridviewTasks.ContainerFromIndex(index) as GridViewItem;
                if (container == null)
                {
                    return;
                }

                var point = container.TransformToVisual(this.mainPage).TransformPoint(new Point());
                intersectingPoint.Y = point.Y + container.ActualHeight / 2;

                element = VisualTreeHelper
                          .FindElementsInHostCoordinates(intersectingPoint, this.gridviewTasks)
                          .OfType <FrameworkElement>()
                          .FirstOrDefault(fe => fe.DataContext is ITask);

                index++;
            }

            ITask task = null;

            if (element != null && element.DataContext is ITask)
            {
                task = (ITask)element.DataContext;
            }

            if (task != null)
            {
                var smartCollection = ((FolderItemViewModel)this.gridviewTasks.DataContext).SmartCollection;

                IFolder folder = task.Folder;
                if (folder.TaskGroup != TaskGroup.Action)
                {
                    Group <ITask> group = smartCollection.Items.FirstOrDefault(g => g.Contains(task));
                    if (group == this.currentTargetGroup)
                    {
                        return;
                    }

                    this.currentTargetGroup = group;

                    // find out the coordinates needed to display a visual indicator
                    var firstItem      = this.currentTargetGroup[0];
                    var firstContainer = (GridViewItem)this.gridviewTasks.ContainerFromItem(firstItem);
                    if (firstContainer == null)
                    {
                        return;
                    }

                    var pA = firstContainer.TransformToVisual(this.dragDropHost).TransformPoint(new Point());

                    var lastItem      = this.currentTargetGroup[this.currentTargetGroup.Count - 1];
                    var lastContainer = (GridViewItem)this.gridviewTasks.ContainerFromItem(lastItem);
                    if (lastContainer == null)
                    {
                        return;
                    }

                    var pB = lastContainer.TransformToVisual(this.dragDropHost).TransformPoint(new Point());

                    pB.X += lastContainer.ActualWidth;
                    pB.Y += lastContainer.ActualHeight;

                    var border = new Border
                    {
                        Background = this.highlightBrush,
                        Opacity    = 0
                    };
                    border.AnimateOpacity(this.BackgroundIndicatorOpacity, TimeSpan.FromMilliseconds(DragDropHostAnimationTimeMs));

                    this.dragDropHost.Child    = border;
                    border.Width               = Math.Abs(pB.X - pA.X);
                    border.Height              = this.dragDropHost.ActualHeight - 20;
                    border.VerticalAlignment   = VerticalAlignment.Top;
                    border.HorizontalAlignment = HorizontalAlignment.Left;
                    border.Margin              = new Thickness(Math.Abs(pA.X), Math.Abs(pA.Y), 0, 0);
                }
            }
        }