Created
September 14, 2009 19:41
-
-
Save olohmann/186880 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class DragDropFriendlyListBoxItem : ListBoxItem | |
| { | |
| private List<object> previousSelection = new List<object>(); | |
| private List<object> currentSelection = new List<object>(); | |
| private bool capturedSelectionOnMouseDown; | |
| /// <summary> | |
| /// Finds the parent DragDropFriendly list box. | |
| /// </summary> | |
| /// <returns>The parent <c>DragDropFriendlyListBox</c>.</returns> | |
| private DragDropFriendlyListBox FindParentDragDropFriendlyListBox() | |
| { | |
| Visual visual = this; | |
| while (visual != null && !(visual is DragDropFriendlyListBox)) | |
| { | |
| visual = (Visual)VisualTreeHelper.GetParent(visual); | |
| } | |
| return visual as DragDropFriendlyListBox; | |
| } | |
| /// <summary> | |
| /// Called when the user presses the left mouse button over the <see cref="T:System.Windows.Controls.ListBoxItem"/>. | |
| /// </summary> | |
| /// <param name="e">The event data.</param> | |
| protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) | |
| { | |
| capturedSelectionOnMouseDown = false; | |
| if (this.IsSelected) | |
| { | |
| previousSelection.Clear(); | |
| currentSelection.Clear(); | |
| // Find the hosting listbox and get the previous selected items. | |
| // That is, the items that are selected *before* the new selection | |
| // has kicked in. | |
| DragDropFriendlyListBox lb = FindParentDragDropFriendlyListBox(); | |
| previousSelection.AddRange(lb.SelectedItems.Cast<object>()); | |
| // Let the handler do its normal work. | |
| base.OnMouseLeftButtonDown(e); | |
| // Cache the new selection and delay it until the mouse-up is received. | |
| currentSelection.AddRange(lb.SelectedItems.Cast<object>()); | |
| lb.UnselectAll(); | |
| previousSelection.ForEach(item => lb.SelectedItems.Add(item)); | |
| capturedSelectionOnMouseDown = true; | |
| } | |
| else | |
| { | |
| base.OnMouseLeftButtonDown(e); | |
| } | |
| } | |
| /// <summary> | |
| /// Raises the <see cref="E:PreviewMouseLeftButtonUp"/> event. | |
| /// </summary> | |
| /// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param> | |
| protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e) | |
| { | |
| // Apply the new selection if required. | |
| if (capturedSelectionOnMouseDown) | |
| { | |
| DragDropFriendlyListBox lb = FindParentDragDropFriendlyListBox(); | |
| lb.UnselectAll(); | |
| currentSelection.ForEach(item => lb.SelectedItems.Add(item)); | |
| } | |
| // Clean up to cache no references. | |
| currentSelection.Clear(); | |
| previousSelection.Clear(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment