Drag and Drop Puzzles



  • I'm trying to move between Canvas part of the picture, and I'm making a mistake The Element is already a logical daughter for another element. First disconnect him. Help me, please.

    code for Drag and Drop

        private void DragImage(object sender, MouseButtonEventArgs e)
        {
            Image image = e.Source as Image;
            DataObject data = new DataObject(typeof(ImageSource), image.Source);
            DragDrop.DoDragDrop(image, data, DragDropEffects.All);
    
    }
    
    
    private void DropImage(object sender, DragEventArgs e)
    {
        Image imageControl = new Image();
        if ((e.Data.GetData(typeof(ImageSource)) != null))
        {
            ImageSource image = e.Data.GetData(typeof(ImageSource)) as ImageSource;
            imageControl = new Image() { Width = 100, Height = 100, Source = image };
        }
        else
        {
            if ((e.Data.GetData(typeof(Image)) != null))
            {
                Image image = e.Data.GetData(typeof(Image)) as Image;
                imageControl = image;
                if (((Canvas)sender).Children.Contains(image))
                {
                    ((Canvas)sender).Children.Remove(image);
                }
            }
        }
    
        imageControl.MouseLeftButtonDown += imageControl_MouseLeftButtonDown;
        ((Canvas)sender).Children.Add(imageControl);
    
    }
    
    void imageControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        Image image = e.Source as Image;
        DataObject data = new DataObject(typeof(Image), image);
        DragDrop.DoDragDrop(image, data, DragDropEffects.All);
    
    }
    


    <Style x:Key="ImagePlacement" TargetType="Canvas">
    <!-- <Setter Property="DataContext" Value="DragAndDrop"/> -->
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="VerticalAlignment" Value="Stretch" />
    <Setter Property="Width" Value="100" />
    <Setter Property="Height" Value="100" />
    <Setter Property="Cursor" Value="Hand" />
    <Setter Property="Background" Value="{StaticResource ResourceKey=placement}" />
    <Setter Property="AllowDrop" Value="True" />
    <Setter Property="BitmapEffect">
    <Setter.Value>
    <OuterGlowBitmapEffect GlowColor="Gold" GlowSize="5" />
    </Setter.Value>
    </Setter>
    <EventSetter Event="Drop" Handler="DropImage" />
    <EventSetter Event="PreviewMouseLeftButtonDown" Handler="imageControl_MouseLeftButtonDown" />
    </Style>
    <Image Width="100"
    Height="100"
    AllowDrop="True"
    PreviewMouseDown="DragImage"
    Source="{Binding Pic, Converter={StaticResource imageConverter}}" />



  • If there is no error, the element should be removed after completion. DragDrop.DoDragDropLike:

    var o = new DataObject(format, itm);
    var res = DragDrop.DoDragDrop(lst, o, DragDropEffects.All);
    if (res == DragDropEffects.Move)
       (lst.ItemsSource as IList).Remove(itm);
    

    Example code - https://ru.stackoverflow.com/a/477112/196972 ♪




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2