【问题标题】:PreviewLeftButtonUp fires too latePreviewLeftButtonUp 触发太晚了
【发布时间】:2020-10-20 20:38:41
【问题描述】:

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

XAML:

<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>

C#:

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);
}

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    你可以像这样使用Mouse.GetPosition()获得grid_Drop方法中的位置:

    private void grid_Drop(object sender, DragEventArgs e)
    {
        Point location = Mouse.GetPosition(grid);
        //do calculation from mouse_move method to get the col and row
        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);
    }
    
    

    【讨论】:

      猜你喜欢
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多