繁体   English   中英

WPF窗口拖放不起作用

[英]WPF Window drag and drop not working

为什么在我将Im​​age img2拖到目标Image image1之后没有更改。

我的XAML代码:

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

这是C#代码:

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

    }

什么都没发生,如何解决这个问题?

您可以像这样直接从删除的文件名信息中读取图像:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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