Guest User

Untitled

a guest
Apr 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. <ItemsControl ItemsSource="{Binding boardSquares}">
  2. <ItemsControl.ItemsPanel>
  3. <ItemsPanelTemplate>
  4. <UniformGrid x:Name="canvasGrid" Background="Black" Columns="5" Rows="5" Margin="5">
  5.  
  6. </UniformGrid>
  7. </ItemsPanelTemplate>
  8. </ItemsControl.ItemsPanel>
  9. <ItemsControl.ItemTemplate>
  10. <DataTemplate>
  11. <Rectangle Fill="Gray" Stroke="BlanchedAlmond" DragEnter="ListBoxDestination_DragEnter" Drop="ListBoxDestination_Drop" />
  12. </DataTemplate>
  13. </ItemsControl.ItemTemplate>
  14. </ItemsControl>
  15.  
  16. private void ListBoxColor_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  17. {
  18. startPoint = e.GetPosition(null);
  19. }
  20.  
  21. private void ListBoxColor_MouseMove(object sender, MouseEventArgs e)
  22. {
  23. Point mousePos = e.GetPosition(null);
  24. Vector diff = startPoint - mousePos;
  25. if (e.LeftButton == MouseButtonState.Pressed &&
  26. (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
  27. Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
  28. {
  29. // get the dragged listbox item
  30. ListBox listBox = (ListBox)sender;
  31. ListBoxItem listBoxItem = findAncestor<ListBoxItem>((DependencyObject)e.OriginalSource);
  32. if (listBoxItem != null)
  33. {
  34. // Find the data behind the listBoxItem
  35. string theItem = listBox.ItemContainerGenerator.ItemFromContainer(listBoxItem).ToString();
  36. // initialize drag and drop
  37. // create a DataObject containing the string to be "dragged"
  38. DataObject dragData = new DataObject(typeof(Rectangle), theItem);
  39. // initialize the dragging
  40. DragDrop.DoDragDrop(listBoxItem, dragData, DragDropEffects.Move);
  41. }
  42. }
  43. }
  44.  
  45. private static T findAncestor<T>(DependencyObject current)
  46. where T : DependencyObject
  47. {
  48. do
  49. {
  50. if (current is T)
  51. {
  52. return (T)current;
  53. }
  54. current = VisualTreeHelper.GetParent(current);
  55. }
  56. while (current != null);
  57. return null;
  58. }
  59.  
  60.  
  61. private void ListBoxDestination_DragEnter(object sender, DragEventArgs e)
  62. {
  63. if (!e.Data.GetDataPresent(typeof(Rectangle)) || sender == e.Source)
  64. {
  65. e.Effects = DragDropEffects.None;
  66. }
  67. }
  68.  
  69. private void ListBoxDestination_Drop(object sender, DragEventArgs e)
  70. {
  71. if (e.Data.GetDataPresent(typeof(string)))
  72. {
  73. Rectangle theItem = (Rectangle)e.Data.GetData(typeof(Rectangle));
  74. Rectangle cellItem = (Rectangle)sender;
  75. cellItem.Fill = cellItem.Fill;
  76. }
  77. }
  78. }
Add Comment
Please, Sign In to add comment