简体   繁体   中英

WinUI3 and Drag and Drop

I am trying to get Drag and Drop working with WinUI3. In WPF I used to add added both AllowDrop and Drop to a UI element and I was able to receive the filename of a dropped file. However in WinUI3 the method Drop is not beeing executed. What am I doing wrong?

XAML

<Page
    x:Class="..."
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="..."
    xmlns:helper="..."
    xmlns:prop="..."
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  <Page.Resources>
    <DataTemplate x:Key="Image"  x:DataType="helper:ProductImage">
      <Border BorderBrush="{x:Bind Path=Color, Mode=OneWay}" BorderThickness="1" Background="Transparent"
              AllowDrop="True" Drop="Image_Drop" >
        <Grid Width="{Binding ElementName=PIImageSize, Path=Value}" 
              Height="{Binding ElementName=PIImageSize, Path=Value}" >
          <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
          </Grid.RowDefinitions>
          <TextBlock Grid.Row="0" Margin="2" HorizontalAlignment="Center"
                     Text="{x:Bind Path=Type, Mode=OneWay}" 
                     Foreground="{x:Bind Path=Color, Mode=OneWay}" />
          <Image Grid.Row="1" Stretch="Uniform" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Source="{x:Bind Path=BitmapImage, Mode=OneWay}"/>
          <TextBox Grid.Row="2" Text="{x:Bind Path=Path, Mode=TwoWay}" />
        </Grid>
        <Border.ContextFlyout>
      </Border.ContextFlyout>
      </Border>
    </DataTemplate>
  </Page.Resources>
...
</Page>

Code

    private void Image_Drop(object sender, DragEventArgs e)
    {
      // Function is not executed
    }

You also have to add the DragOver event, to accept the operation:

private void Image_DragOver(object sender, DragEventArgs e)
{
    e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Copy;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM