【问题标题】:WPF Window drag and drop not workingWPF窗口拖放不起作用
【发布时间】:2017-09-18 10:59:30
【问题描述】:

为什么我拖动 Image 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);

    }

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

【问题讨论】:

    标签: c# wpf xaml drag-and-drop


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-09-23
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 2015-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多