拖放到网格中而不是正确拖放

时间:2016-01-24 17:46:00

标签: c# wpf image drag-and-drop

我在这里找到了代码:

  

https://social.msdn.microsoft.com/Forums/en-US/b01a66ef-ae83-4891-9fe9-74ed2f4fd99c/drag-and-drop-images?forum=wpf

这似乎真正需要的是拖放到具有列定义的网格中。

所以我有:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
    <Grid PreviewMouseLeftButtonDown="Grid_PreviewMouseLeftButtonDown" Background="Transparent"
      PreviewMouseMove="Grid_PreviewMouseMove"
      DragEnter="Grid_DragEnter"
      Drop="Grid_Drop"
      AllowDrop="True">
      <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
      </Grid.RowDefinitions>
      <Image x:Name="Ld" Margin="5" Grid.Row="0" Grid.Column="0" Source="greenBall.ico"/>
      <Image x:Name="iar" Margin="5" Grid.Row="1" Grid.Column="0" Source="greenBall.ico"/>
      <Image x:Name="ry" Margin="5" Grid.Row="0" Grid.Column="1" Source="greenBall.ico"/>
      <Image x:Name="dia" Margin="5" Grid.Row="1" Grid.Column="1" Source="greenBall.ico"/>
   </Grid>

效果是图片中的那个

enter image description here

背后的代码是:

public partial class MainWindow : Window
{

  private Point startDragPoint;
  Image dragImage = new Image();

  public MainWindow()
  {
    InitializeComponent();
  }

  private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  {
    dragImage = e.Source as Image;
    startDragPoint = e.GetPosition(null);
  }

  private void Grid_PreviewMouseMove(object sender, MouseEventArgs e)
  {
    if (e.LeftButton == MouseButtonState.Pressed)
    {
      Point position = e.GetPosition(null);

      if (Math.Abs(position.X - startDragPoint.X) >
             SystemParameters.MinimumHorizontalDragDistance ||
          Math.Abs(position.Y - startDragPoint.Y) >
             SystemParameters.MinimumVerticalDragDistance)

      {
        DataObject data = new DataObject(typeof(ImageSource), dragImage.Source);
        DragDrop.DoDragDrop(dragImage, data, DragDropEffects.Move);
      }
    }
  }

  private void Grid_DragEnter(object sender, DragEventArgs e)
  {
    if (e.Data.GetDataPresent(typeof(DataObject)))
    {
      e.Effects = DragDropEffects.Copy;
    }
    else
    {
      e.Effects = DragDropEffects.None;
    }
}

  private void Grid_Drop(object sender, DragEventArgs e)
  {
    Image dropImage = e.Source as Image;
    double x = dropImage.ActualWidth;

    dragImage.Source = dropImage.Source;
    dropImage.Source = e.Data.GetData(typeof(ImageSource)) as ImageSource;

  }
 }

现在,当我拖放红球时,效果就是没有出现

enter image description here

我不是WPF专家,但我看到dropImage.source为空

enter image description here

没有必要保持相同的代码我所需要的是将图像拖放到网格列和行定义 提前致谢

0 个答案:

没有答案