private void DisplayList_PreviewMouseMove(object sender, MouseEventArgs e) { //if the left mouse button is not pressed, then we can't be dragging anything if (e.LeftButton != MouseButtonState.Pressed || !(sender is ListBox) || (_clickedGame == null)) { return; } var mousePos = e.GetPosition(null); var diff = _startPoint - mousePos; //If we travel far enough, begin the drag if (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance) { _clickedGame = null; foreach (object selItem in _selItems) { if (!DisplayList.SelectedItems.Contains(selItem)) { DisplayList.SelectedItems.Add(selItem); } } //The sender is always the list box var listBox = (ListBox)sender; var items = listBox.SelectedItems.OfType <RemoteSteamApp>().ToArray(); FriendTransfer transferData = new FriendTransfer((FriendViewModel)listBox.DataContext, items); DataObject dragData = new DataObject(DRAG_DATA_NAME, transferData); DragDrop.DoDragDrop(this, dragData, DragDropEffects.Move); } }
private void DisplayList_PreviewMouseMove(object sender, MouseEventArgs e) { //if the left mouse button is not pressed, then we can't be dragging anything if (e.LeftButton != MouseButtonState.Pressed || !(_mousePressOrigSource is DependencyObject)) { return; } if (!(sender is ListBox)) { return; } var mousePos = e.GetPosition(null); var diff = _startPoint - mousePos; //If we travel far enough, begin the drag if (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance) { //The sender is always the list box var listBox = (ListBox)sender; //Find a ListBoxItem ancestor of the mouse press original source (the top hit-detection item, not the item that handled the event) var listBoxItem = FindAncestor <ListBoxItem>((DependencyObject)_mousePressOrigSource); if (listBoxItem != null && listBoxItem.DataContext is RemoteSteamApp) { var item = listBoxItem.DataContext; FriendTransfer transferData = new FriendTransfer((FriendViewModel)listBox.DataContext, (RemoteSteamApp)listBoxItem.DataContext); DataObject dragData = new DataObject(DRAG_DATA_NAME, transferData); DragDrop.DoDragDrop(listBoxItem, dragData, DragDropEffects.Move); } } }