/// <summary> /// Adds all selected items when drag operation begins. /// </summary> /// <param name="eventArgs">Information about the event.</param> protected override void OnItemDragStarting(ItemDragEventArgs eventArgs) { SelectionCollection selectionCollection = new SelectionCollection(this.DataGrid.SelectedItems.OfType <object>()); eventArgs.Data = selectionCollection; CardPanel cardPanel = new CardPanel(); IEnumerable <DataGridRow> itemContainers = (from item in selectionCollection.Select(selection => selection.Item) let row = this.DataGrid .GetVisualDescendants() .OfType <DataGridRow>() .FirstOrDefault() where row != null select row); foreach (DataGridRow row in itemContainers) { cardPanel.Children.Add(new Image { Source = new WriteableBitmap(row, new TranslateTransform()) }); } eventArgs.DragDecoratorContent = cardPanel; eventArgs.Handled = true; base.OnItemDragStarting(eventArgs); }
/// <summary> /// Called when the value of the VerticalMargin property changes. /// </summary> /// <param name="d">Control that changed its VerticalMargin.</param> /// <param name="e">Event arguments.</param> private static void OnVerticalMarginPropertyChanged(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e) { CardPanel source = (CardPanel)d; double oldValue = (double)e.OldValue; double newValue = (double)e.NewValue; source.OnVerticalMarginPropertyChanged(oldValue, newValue); }
/// <summary> /// Adds all selected items when drag operation begins. /// </summary> /// <param name="eventArgs">Information about the event.</param> protected override void OnItemDragStarting(ItemDragEventArgs eventArgs) { SelectionCollection selectionCollection = new SelectionCollection(); // If panel is virtualized there is no way of knowing the precise // index of each selected item. Panel itemsHost = this.ListBox.GetItemsHost(); if (itemsHost is VirtualizingPanel) { foreach (object item in this.ListBox.SelectedItems) { selectionCollection.Add(new Selection(item)); } // Adding the item dragged even if it isn't selected SelectionCollection defaultSelectionCollection = SelectionCollection.ToSelectionCollection(eventArgs.Data); if (defaultSelectionCollection.Count == 1 && !selectionCollection.Any(selection => object.Equals(selection.Item, defaultSelectionCollection[0].Item))) { selectionCollection.Add(defaultSelectionCollection[0]); } } else { for (int cnt = 0; cnt < this.ListBox.Items.Count; cnt++) { ListBoxItem listBoxItem = this.ListBox.ItemContainerGenerator.ContainerFromIndex(cnt) as ListBoxItem; if (listBoxItem.IsSelected) { selectionCollection.Add(new Selection(cnt, this.ListBox.Items[cnt])); } } // Adding the item dragged even if it isn't selected SelectionCollection defaultSelectionCollection = GetSelectionCollection(eventArgs.Data); if (defaultSelectionCollection.Count == 1) { if (selectionCollection.All(selection => selection.Index != defaultSelectionCollection[0].Index)) { selectionCollection.Add(defaultSelectionCollection[0]); } } } eventArgs.Data = selectionCollection; CardPanel cardPanel = new CardPanel(); IEnumerable <UIElement> itemContainers = selectionCollection.SelectedItems .Select(item => this.ListBox.ItemContainerGenerator.ContainerFromItem(item)) .Where(item => item != null) .OfType <UIElement>(); foreach (ListBoxItem row in itemContainers) { cardPanel.Children.Add(new Image { Source = new WriteableBitmap(row, new TranslateTransform()) }); } eventArgs.DragDecoratorContent = cardPanel; eventArgs.Handled = true; base.OnItemDragStarting(eventArgs); }