Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <ItemsControl ItemsSource="{Binding boardSquares}">
- <ItemsControl.ItemsPanel>
- <ItemsPanelTemplate>
- <UniformGrid x:Name="canvasGrid" Background="Black" Columns="5" Rows="5" Margin="5">
- </UniformGrid>
- </ItemsPanelTemplate>
- </ItemsControl.ItemsPanel>
- <ItemsControl.ItemTemplate>
- <DataTemplate>
- <Rectangle Fill="Gray" Stroke="BlanchedAlmond" DragEnter="ListBoxDestination_DragEnter" Drop="ListBoxDestination_Drop" />
- </DataTemplate>
- </ItemsControl.ItemTemplate>
- </ItemsControl>
- private void ListBoxColor_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- startPoint = e.GetPosition(null);
- }
- private void ListBoxColor_MouseMove(object sender, MouseEventArgs e)
- {
- Point mousePos = e.GetPosition(null);
- Vector diff = startPoint - mousePos;
- if (e.LeftButton == MouseButtonState.Pressed &&
- (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
- Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
- {
- // get the dragged listbox item
- ListBox listBox = (ListBox)sender;
- ListBoxItem listBoxItem = findAncestor<ListBoxItem>((DependencyObject)e.OriginalSource);
- if (listBoxItem != null)
- {
- // Find the data behind the listBoxItem
- string theItem = listBox.ItemContainerGenerator.ItemFromContainer(listBoxItem).ToString();
- // initialize drag and drop
- // create a DataObject containing the string to be "dragged"
- DataObject dragData = new DataObject(typeof(Rectangle), theItem);
- // initialize the dragging
- DragDrop.DoDragDrop(listBoxItem, dragData, DragDropEffects.Move);
- }
- }
- }
- private static T findAncestor<T>(DependencyObject current)
- where T : DependencyObject
- {
- do
- {
- if (current is T)
- {
- return (T)current;
- }
- current = VisualTreeHelper.GetParent(current);
- }
- while (current != null);
- return null;
- }
- private void ListBoxDestination_DragEnter(object sender, DragEventArgs e)
- {
- if (!e.Data.GetDataPresent(typeof(Rectangle)) || sender == e.Source)
- {
- e.Effects = DragDropEffects.None;
- }
- }
- private void ListBoxDestination_Drop(object sender, DragEventArgs e)
- {
- if (e.Data.GetDataPresent(typeof(string)))
- {
- Rectangle theItem = (Rectangle)e.Data.GetData(typeof(Rectangle));
- Rectangle cellItem = (Rectangle)sender;
- cellItem.Fill = cellItem.Fill;
- }
- }
- }
Add Comment
Please, Sign In to add comment