简体   繁体   中英

WPF Window drag and drop not working

Why destination Image image1 is not changing after I drag Image img2 on it.

My XAML Code:

<Window x:Class="WpfApplication4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="463.42" Width="861.214">
<Grid>

    <Image Name="image1" Margin="291,10,297,94" AllowDrop="True" Source="C:/img/0.png" Drop="img1_Drop"/>

    <Button x:Name="btn1" Content="merge" HorizontalAlignment="Left" Margin="753,392,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    <Button Content="add" HorizontalAlignment="Left" Margin="658,392,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
</Grid>

Here is C# code:

private void img1_Drop(object sender, DragEventArgs e)
    {

        image1.Source = (System.Windows.Media.ImageSource)e.Data.GetData(typeof(System.Windows.Media.ImageSource));
        //image1.Source = (BitmapImage)e.Data.GetData(DataFormats.Bitmap);

    }

Nothing happens, how to solve this problem ?

You can read the image directly from dropped filename info like this:

if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
    var filename = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
    image1.Source = new BitmapImage(new Uri(filename));
}

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