Пример #1
0
        public static FrameworkElement GetItemContainer(ItemsControl ItemsControl,
                                                        Visual BottomMostVisual)
        {
            FrameworkElement itemContainer = null;

            if (ItemsControl != null && BottomMostVisual != null && ItemsControl.Items.Count >= 1)
            {
                var firstContainer = ItemsControl.ItemContainerGenerator.ContainerFromIndex(0);

                if (firstContainer == null)
                {
                    return(itemContainer);                              // retyrn null;
                }

                itemContainer = DynamicFormUtilities.FindAncestor(firstContainer.GetType(),
                                                                  BottomMostVisual);

                if (itemContainer == null || itemContainer.DataContext == null)
                {
                    return(itemContainer);
                }

                var itemContainerVerify = ItemsControl.ItemContainerGenerator.ContainerFromItem(itemContainer.
                                                                                                DataContext) as FrameworkElement;

                if (itemContainer != itemContainerVerify)
                {
                    return(null);
                }
            }

            return(itemContainer);
        }
Пример #2
0
        private void DropTarget_PreviewDrop(object sender, DragEventArgs e)
        {
            logger.Trace("Entered DropTarget_PreviewDrop()");

            var draggedItem  = e.Data.GetData(Format.Name);
            var indexRemoved = -1;

            if (draggedItem != null)
            {
                if ((e.Effects & DragDropEffects.Move) != DragDropEffects.None)
                {
                    indexRemoved = DynamicFormUtilities.RemoveItemFromItemsControl(
                        SourceItemsControl, draggedItem);
                }

                if (indexRemoved != -1 && SourceItemsControl == TargetItemsControl &&
                    indexRemoved < InsertionIndex)
                {
                    InsertionIndex--;
                }

                DynamicFormUtilities.InsertItemInItemsControl(TargetItemsControl,
                                                              draggedItem, InsertionIndex);
                RemoveDraggedAdorner();
                RemoveInsertionAdorner();
            }
            e.Handled = true;
        }
Пример #3
0
        private void DragSource_PreviewMouseLeftButtonDown(object sender,
                                                           MouseButtonEventArgs e)
        {
            var fe = DynamicFormUtilities.GetItemContainer(sender as ItemsControl,
                                                           e.OriginalSource as
                                                           Visual) as FrameworkElement;

            if (fe != null && fe.DataContext is PropertyInformation &&
                (fe.DataContext as PropertyInformation
                ).HasBeenUsed)
            {
                e.Handled = true;
                return;
            }

            InitialMousePosition = e.GetPosition(TopWindow);
            SourceItemsControl   = (ItemsControl)sender;
            TopWindow            = DynamicFormUtilities.FindAncestor(typeof(Window),
                                                                     SourceItemsControl) as Window;
            SourceItemContainer = DynamicFormUtilities.GetItemContainer(
                SourceItemsControl, e.OriginalSource
                as Visual);

            if (SourceItemContainer != null)
            {
                DraggedData = SourceItemContainer.DataContext;
            }
        }
Пример #4
0
        private void DragSource_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            logger.Trace("Entered DragSource_PreviewMouseMove()");

            if (DraggedData == null ||
                !DynamicFormUtilities.IsMovementBigEnough(InitialMousePosition,
                                                          e.GetPosition(TopWindow)))
            {
                return;
            }

            var data = new DataObject(Format.Name, DraggedData);
            var previousAllowDrop = TopWindow.AllowDrop;

            TopWindow.AllowDrop  = true;
            TopWindow.DragEnter += TopWindow_DragEnter;
            TopWindow.DragOver  += TopWindow_DragOver;
            TopWindow.DragLeave += TopWindow_DragLeave;

            var effects = DragDrop.DoDragDrop(sender as DependencyObject, data,
                                              DragDropEffects.Move);

            TopWindow.AllowDrop  = previousAllowDrop;
            TopWindow.DragEnter -= TopWindow_DragEnter;
            TopWindow.DragOver  -= TopWindow_DragOver;
            TopWindow.DragLeave -= TopWindow_DragLeave;
            DraggedData          = null;
        }
Пример #5
0
        private void DecideDropTarget(DragEventArgs e)
        {
            logger.Trace("Entered DecideDropTarget()");

            var targetItemsControlCount = TargetItemsControl.Items.Count;
            var draggedItem             = e.Data.GetData(Format.Name);

            if (IsDropDataTypeAllowed(draggedItem))
            {
                if (targetItemsControlCount > 0)
                {
                    HasVerticalOrientation = DynamicFormUtilities.HasVerticalOrientation(
                        TargetItemsControl.
                        ItemContainerGenerator.ContainerFromIndex(0) as FrameworkElement);

                    TargetItemContainer = DynamicFormUtilities.GetItemContainer(
                        TargetItemsControl, e.
                        OriginalSource as Visual);

                    if (TargetItemContainer != null)
                    {
                        IsInFirstHalf = DynamicFormUtilities.IsInFirstHalf(TargetItemContainer,
                                                                           e.GetPosition(TargetItemContainer),
                                                                           HasVerticalOrientation);
                        InsertionIndex =
                            TargetItemsControl.ItemContainerGenerator.IndexFromContainer(
                                TargetItemContainer);

                        if (!IsInFirstHalf)
                        {
                            InsertionIndex++;
                        }
                    }
                    else
                    {
                        TargetItemContainer =
                            TargetItemsControl.ItemContainerGenerator.ContainerFromIndex(
                                targetItemsControlCount - 1) as FrameworkElement;
                        IsInFirstHalf  = false;
                        InsertionIndex = targetItemsControlCount;
                    }
                }
                else
                {
                    TargetItemContainer = null;
                    InsertionIndex      = 0;
                }
            }
            else
            {
                TargetItemContainer = null;
                InsertionIndex      = -1;
                e.Effects           = DragDropEffects.None;
            }
        }