Пример #1
0
        public DragInfo(object sender, MouseButtonEventArgs e)
        {
            //use the relative mouse position for browser
            DragStartPosition = Mouse.GetPosition(sender as IInputElement);
            Effects           = DragDropEffects.None;
            MouseButton       = e.ChangedButton;
            VisualSource      = sender as UIElement;
            ItemsControl itemsControl = sender as ItemsControl;

            if (itemsControl != null)
            {
                UIElement item = null;
                // Don't remove try/catch, sometimes exception is thrown here.
                // For example, when clicking "Loading more..." item, the item will be removed immediately,
                // so when code goes here, the item has gone and exception is thrown.
                try
                {
                    item = itemsControl.GetItemContainer((UIElement)e.OriginalSource);
                }
                catch
                {
                }

                if (item != null)
                {
                    ItemsControl itemParent = ItemsControl.ItemsControlFromItemContainer(item);

                    if (itemParent != null)
                    {
                        SourceCollection = itemParent.ItemsSource ?? itemParent.Items;
                        SourceItem       = itemParent.ItemContainerGenerator.ItemFromContainer(item);
                    }
                    SourceItems = itemsControl.GetSelectedItems();

                    // Some controls (TreeView) haven't updated their
                    // SelectedItem by this point. Check to see if there 1 or less item in
                    // the SourceItems collection, and if so, override the SelectedItems
                    // with the clicked item.
                    if (SourceItems.Cast <object>().Count() <= 1)
                    {
                        SourceItems = Enumerable.Repeat(SourceItem, 1);
                    }

                    VisualSourceItem = item;
                }
                else
                {
                    SourceCollection = itemsControl.ItemsSource ?? itemsControl.Items;
                }
            }

            if (SourceItems == null)
            {
                SourceItems = Enumerable.Empty <object>();
            }
        }
Пример #2
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public DragInfo(object sender, MouseButtonEventArgs e)
        {
            DragStartPosition = e.GetPosition(null);
            Effects           = DragDropEffects.None;
            MouseButton       = e.ChangedButton;
            VisualSource      = sender as UIElement;

            if (sender is ItemsControl)
            {
                ItemsControl itemsControl = (ItemsControl)sender;
                UIElement    item         = itemsControl.GetItemContainer((UIElement)e.OriginalSource);

                if (item != null)
                {
                    ItemsControl itemParent = ItemsControl.ItemsControlFromItemContainer(item);

                    SourceCollection = itemParent.ItemsSource ?? itemParent.Items;
                    SourceItem       = itemParent.ItemContainerGenerator.ItemFromContainer(item);
                    SourceItems      = itemsControl.GetSelectedItems();

                    // Some controls (I'm looking at you TreeView!) haven't updated their
                    // SelectedItem by this point. Check to see if there 1 or less item in
                    // the SourceItems collection, and if so, override the SelectedItems
                    // with the clicked item.
                    if (SourceItems.Cast <object>().Count() <= 1)
                    {
                        SourceItems = Enumerable.Repeat(SourceItem, 1);
                    }

                    VisualSourceItem = item;
                }
                else
                {
                    SourceCollection = itemsControl.ItemsSource ?? itemsControl.Items;
                }
            }

            if (SourceItems == null)
            {
                SourceItems = Enumerable.Empty <object>();
            }

            this.IsNotHandled = false;
        }
Пример #3
0
        private void PopulateInfoFromItemControl(RoutedEventArgs e, ItemsControl control)
        {
            var itemsControl = control;
            var item         = itemsControl.GetItemContainer((UIElement)e.OriginalSource);

            if (item == null)
            {
                SourceCollection = itemsControl.ItemsSource ?? itemsControl.Items;
                return;
            }

            var itemParent = ItemsControl.ItemsControlFromItemContainer(item);

            SourceCollection = itemParent.ItemsSource ?? itemParent.Items;
            SourceItem       = itemParent.ItemContainerGenerator.ItemFromContainer(item);
            SourceItems      = itemsControl.GetSelectedItems();

            if (SourceItems.Cast <object>().Count() <= 1)
            {
                SourceItems = Enumerable.Repeat(SourceItem, 1);
            }

            VisualSourceItem = item;
        }
        /// <summary>
        ///     Initializes a new instance of the DragInfo class.
        /// </summary>
        /// <param name="sender">
        ///     The sender of the mouse event that initiated the drag.
        /// </param>
        /// <param name="e">
        ///     The mouse event that initiated the drag.
        /// </param>
        public DragInfo(object sender, MouseButtonEventArgs e)
        {
            DragStartPosition = e.GetPosition((IInputElement)sender);
            Effects           = DragDropEffects.None;
            MouseButton       = e.ChangedButton;
            VisualSource      = sender as UIElement;

            var itemsControl = sender as ItemsControl;

            if (itemsControl != null)
            {
                var lSourceItem = e.OriginalSource as UIElement;
                // If we can't cast object as a UIElement it might be a FrameworkContentElement, if so try and use its parent.
                if (lSourceItem == null && e.OriginalSource is FrameworkContentElement)
                {
                    lSourceItem = ((FrameworkContentElement)e.OriginalSource).Parent as UIElement;
                }
                UIElement lItem = null;
                if (lSourceItem != null)
                {
                    lItem = itemsControl.GetItemContainer(lSourceItem);
                }
                if (lItem == null)
                {
                    lItem = itemsControl.GetItemContainerAt(
                        e.GetPosition(itemsControl),
                        itemsControl.GetItemsPanelOrientation());
                }

                if (lItem != null)
                {
                    // Remember the relative position of the item being dragged
                    PositionInDraggedItem = e.GetPosition(lItem);

                    var lItemParent = ItemsControl.ItemsControlFromItemContainer(lItem);

                    if (lItemParent != null)
                    {
                        SourceCollection = lItemParent.ItemsSource ?? lItemParent.Items;
                        SourceItem       = lItemParent.ItemContainerGenerator.ItemFromContainer(lItem);
                    }
                    SourceItems = itemsControl.GetSelectedItems();

                    // Some controls (I'm looking at you TreeView!) haven't updated their
                    // SelectedItem by this point. Check to see if there 1 or less item in
                    // the SourceItems collection, and if so, override the control's
                    // SelectedItems with the clicked item.
                    if (SourceItems.Cast <object>().Count() <= 1)
                    {
                        SourceItems = Enumerable.Repeat(SourceItem, 1);
                    }

                    VisualSourceItem = lItem;
                }
                else
                {
                    SourceCollection = itemsControl.ItemsSource ?? itemsControl.Items;
                }
            }
            else
            {
                var element = sender as UIElement;
                if (element != null)
                {
                    PositionInDraggedItem = e.GetPosition(element);
                }
            }

            if (SourceItems == null)
            {
                SourceItems = Enumerable.Empty <object>();
            }
        }