C# PreviewLeftButtonP启动太晚

C# PreviewLeftButtonP启动太晚,c#,wpf,C#,Wpf,我正在制作一个简单的程序,可以将元素拖放到网格中,但它们的位置取决于特定的列和行。因此,我尝试使用previewLeftButtonP事件查找光标指向的位置,但它在Drop事件后触发,元素放置在错误的位置 XAML: C#: private void img\u MouseDown(对象发送器,MouseButtonEventArgs e) { 图像img=(图像)发送方; DataObject DataObject=新的DataObject(typeof(ImageSource),img.

我正在制作一个简单的程序,可以将元素拖放到网格中,但它们的位置取决于特定的列和行。因此,我尝试使用previewLeftButtonP事件查找光标指向的位置,但它在Drop事件后触发,元素放置在错误的位置

XAML:


C#:

private void img\u MouseDown(对象发送器,MouseButtonEventArgs e)
{
图像img=(图像)发送方;
DataObject DataObject=新的DataObject(typeof(ImageSource),img.Source);
DoDragDrop.DoDragDrop(img、dataObject、DragDropEffects.All);
}
专用void grid\u MouseMove(对象发送器,MouseEventArgs e)
{
双y=e.GetPosition(网格).y;
双启动=0.0;
行=0;
foreach(grid.RowDefinitions中的RowDefinition rd)
{
开始+=rd.实际高度;
如果(y<开始)
{
打破
}
行++;
}
双x=e.GetPosition(网格).x;
开始=0.0;
col=0;
foreach(grid.ColumnDefinitions中的ColumnDefinition cd)
{
开始+=cd.实际宽度;
如果(x<开始)
{
打破
}
col++;
}
}
专用无效网格(对象发送器,DragEventArgs e)
{
图像控制=新图像{Stretch=Stretch.Fill};
if((e.Data.GetData(typeof(ImageSource))!=null))
{
ImageSource image=e.Data.GetData(typeof(ImageSource))作为ImageSource;
imageControl=newimage(){Stretch=Stretch.Fill,Source=Image};
}
其他的
{
if((e.Data.GetData(typeof(Image))!=null))
{
Image Image=e.Data.GetData(type of(Image))作为图像;
图像控制=图像;
if(((网格)sender.Children.Contains(图像))
{
((网格)发送器)。子对象。删除(图像);
}
}
}
Grid.SetColumn(图像控制,col);
Grid.SetRow(图像控件,行);
((网格)sender.Children.Add(imageControl);
}

您可以使用
鼠标.GetPosition()
网格中获得位置,如下所示:

private void grid\u Drop(对象发送方,DragEventArgs e)
{
点位置=鼠标.GetPosition(网格);
//使用鼠标移动方法进行计算,以获得列和行
图像控制=新图像{Stretch=Stretch.Fill};
if((e.Data.GetData(typeof(ImageSource))!=null))
{
ImageSource image=e.Data.GetData(typeof(ImageSource))作为ImageSource;
imageControl=newimage(){Stretch=Stretch.Fill,Source=Image};
}
其他的
{
if((e.Data.GetData(typeof(Image))!=null))
{
Image Image=e.Data.GetData(type of(Image))作为图像;
图像控制=图像;
if(((网格)sender.Children.Contains(图像))
{
((网格)发送器)。子对象。删除(图像);
}
}
}
Grid.SetColumn(图像控制,col);
Grid.SetRow(图像控件,行);
((网格)sender.Children.Add(imageControl);
}
<Window x:Class="Pazzles.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:Pazzles"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Menu x:Name="menu"  VerticalAlignment="Top" Grid.Row="0">
            <MenuItem Header="Розбити зображення">
                <MenuItem Header="Відкрити" Click="OpenImage_Click"/>
                <MenuItem Header="Розрізати зображення" Click="CutImage_Click"/>
            </MenuItem>
            <MenuItem Header="Зібрати пазл" Click ="OpenCatalog_Click">
            </MenuItem>
        </Menu>
        <Grid Grid.Row="1" Name="layout">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <ScrollViewer>
                <StackPanel x:Name="stackPanel" Width="150" Grid.Column="0"/>
            </ScrollViewer>
            
            <GridSplitter Grid.Column="1" ShowsPreview="False" Width="3" VerticalAlignment="Stretch" HorizontalAlignment="Center"/>
            <Grid x:Name="grid" Background="Transparent" Grid.Column="2" ShowGridLines="True" AllowDrop="True" PreviewMouseLeftButtonUp="grid_MouseMove" Drop ="grid_Drop">
                
            </Grid>
        </Grid>
    </Grid>
</Window>
private void img_MouseDown(object sender, MouseButtonEventArgs e)
{
   Image img = (Image)sender;
   DataObject dataObject = new DataObject(typeof(ImageSource), img.Source);
   DragDrop.DoDragDrop(img, dataObject, DragDropEffects.All);
}

private void grid_MouseMove(object sender, MouseEventArgs e)
{
   double y = e.GetPosition(grid).Y;
   double start = 0.0;
   row = 0;
   foreach (RowDefinition rd in grid.RowDefinitions)
   {
      start += rd.ActualHeight;
      if (y < start)
      {
         break;
      }
      row++;
   }
   double x = e.GetPosition(grid).X;
   start = 0.0;
   col = 0;
   foreach (ColumnDefinition cd in grid.ColumnDefinitions)
   {
      start += cd.ActualWidth;
      if (x < start)
      {
         break;
      }
      col++;
   }
}

private void grid_Drop(object sender, DragEventArgs e)
{
   Image imageControl = new Image { Stretch = Stretch.Fill };
   if ((e.Data.GetData(typeof(ImageSource)) != null))
   {
      ImageSource image = e.Data.GetData(typeof(ImageSource)) as ImageSource;
      imageControl = new Image() { Stretch = Stretch.Fill, Source = image };
   }
   else
   {
      if ((e.Data.GetData(typeof(Image)) != null))
      {
         Image image = e.Data.GetData(typeof(Image)) as Image;
         imageControl = image;
         if (((Grid)sender).Children.Contains(image))
         {
            ((Grid)sender).Children.Remove(image);
         }
      }
   }
   Grid.SetColumn(imageControl, col);
   Grid.SetRow(imageControl, row);

   ((Grid)sender).Children.Add(imageControl);
}