示例#1
0
        public DraggedItem(ItemVisualizer sourceItemVisualizer)
        {
            DropOnStashEffect = DragDropEffects.Move;
            DropOnInventoryEffect = DragDropEffects.Copy;
            DropOnBinEffect = DragDropEffects.Move;
            SourceItemVisualizer = sourceItemVisualizer;

            var mainWindow = (MetroWindow) Application.Current.MainWindow;
            var dragScope = (UIElement)mainWindow.Content;

            var imageSource = sourceItemVisualizer.Item.Image.ImageSource.Result;
            var brush = new ImageBrush(imageSource) { Opacity = 0.5 };
            _dragAdorner = new DragAdorner(dragScope, new Size(imageSource.Width, imageSource.Height), brush);
            DragStart = Mouse.GetPosition(SourceItemVisualizer);
            // DragStart is subtracted for the cursor to be on the item image at the same position as where the drag
            // was initiated.
            // The window position is subtracted because the cursor position is retrieved as an absolute value.
            // The title bar height is subtracted because the content used for the adorner does not include it.
            var winPos = GetWindowPosition(mainWindow);
            _offset.X = -DragStart.X - winPos.X;
            _offset.Y = -DragStart.Y - winPos.Y - mainWindow.TitlebarHeight;

            _adornerLayer = AdornerLayer.GetAdornerLayer(dragScope);
            _adornerLayer.Add(_dragAdorner);

            DragDrop.AddGiveFeedbackHandler(sourceItemVisualizer, GiveFeedback);
        }
示例#2
0
        public DraggedItem(ItemVisualizer sourceItemVisualizer)
        {
            DropOnStashEffect     = DragDropEffects.Move;
            DropOnInventoryEffect = DragDropEffects.Copy;
            DropOnBinEffect       = DragDropEffects.Move;
            SourceItemVisualizer  = sourceItemVisualizer;

            var mainWindow = (MetroWindow)Application.Current.MainWindow;
            var dragScope  = (UIElement)mainWindow.Content;

            var imageSource = sourceItemVisualizer.Item.Image.ImageSource.Result;
            var brush       = new ImageBrush(imageSource)
            {
                Opacity = 0.5
            };

            _dragAdorner = new DragAdorner(dragScope, new Size(imageSource.Width, imageSource.Height), brush);
            DragStart    = Mouse.GetPosition(SourceItemVisualizer);
            // DragStart is subtracted for the cursor to be on the item image at the same position as where the drag
            // was initiated.
            // The window position is subtracted because the cursor position is retrieved as an absolute value.
            // The title bar height is subtracted because the content used for the adorner does not include it.
            var winPos = GetWindowPosition(mainWindow);

            _offset.X = -DragStart.X - winPos.X;
            _offset.Y = -DragStart.Y - winPos.Y - mainWindow.TitlebarHeight;

            _adornerLayer = AdornerLayer.GetAdornerLayer(dragScope);
            _adornerLayer.Add(_dragAdorner);

            // Win32.GetCursorPos() returns logical pixels, WPF uses physical/device pixels.
            // Need to scale from logical to physical if user has a custom UI scaling.
            _uiScaling = PresentationSource.FromVisual(_dragAdorner).CompositionTarget.TransformToDevice;

            DragDrop.AddGiveFeedbackHandler(sourceItemVisualizer, GiveFeedback);
        }
示例#3
0
 private void InitialiseAdorner(UIElement listViewItem)
 {
     var brush = new VisualBrush(listViewItem);
     _adorner = new DragAdorner(listViewItem, listViewItem.RenderSize, brush) { Opacity = 0.5 };
     _layer = AdornerLayer.GetAdornerLayer(lvSavedBuilds);
     _layer.Add(_adorner);
 }
示例#4
0
        private void BeginDrag(MouseEventArgs e)
        {
            var listView = lvSavedBuilds;
            var listViewItem = ((DependencyObject)e.OriginalSource).FindAnchestor<ListViewItem>();

            if (listViewItem == null)
                return;

            // get the data for the ListViewItem
            var item = listView.ItemContainerGenerator.ItemFromContainer(listViewItem);

            //setup the drag adorner.
            InitialiseAdorner(listViewItem);

            //add handles to update the adorner.
            listView.PreviewDragOver += ListViewDragOver;
            listView.DragLeave += ListViewDragLeave;
            listView.DragEnter += ListViewDragEnter;

            var data = new DataObject("myFormat", item);
            DragDrop.DoDragDrop(lvSavedBuilds, data, DragDropEffects.Move);

            //cleanup
            listView.PreviewDragOver -= ListViewDragOver;
            listView.DragLeave -= ListViewDragLeave;
            listView.DragEnter -= ListViewDragEnter;

            if (_adorner != null)
            {
                AdornerLayer.GetAdornerLayer(listView).Remove(_adorner);
                _adorner = null;
                SaveBuildsToFile();
            }
        }