2010-10-23 95 views
0

以下是我在wpf中拖放的代碼。我在windowform中使用相同的代碼拖放圖像,但我似乎無法爲wpf工作你能幫忙嗎?在WPF中拖放C#

<Image Height="464" Name="PictureBox" Stretch="Uniform" Width="769" AllowDrop="True" DragEnter="PictureBox_DragEnter" Drop="PictureBox_Drop" /> 


    private void PictureBox_DragEnter(object sender, DragEventArgs e) 

    { 
     if (e.Data.GetDataPresent(DataFormats.Bitmap, false) == true) 
      e.Effects = DragDropEffects.None; 
    } 

    private void PictureBox_Drop(object sender, DragEventArgs e) 
    { 
     string[] files = (string[])e.Data.GetData(DataFormats.Bitmap); 
     // images is (arraylist for multi pictures. 
     if (images == null) 
      images = new ArrayList(); 
     for (int i = 0; i < files.Length; i++) 
     { 
      BitmapImage bitmap = new BitmapImage(); 
      bitmap.BeginInit(); 
      bitmap.UriSource = new Uri(files[i]); 
      bitmap.EndInit(); 
      images.Add(bitmap);    
     } 
     currentPicture = images.Count; 
     btn_Next_Click(sender, e); 
     MessageBox.Show("images " + images.Count); 
    } 

回答

0

在我看來這個代碼是不正確的:

if (e.Data.GetDataPresent(DataFormats.Bitmap, false) == true) 
     e.Effects = DragDropEffects.None; 

檢查==假。

此外,DataFormats.Bitmap有圖像類型,而不是字符串[]。

我就這麼代碼時:

private void DragSource_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) 
{ 
    //... 
    DataObject data = new DataObject("Object", new string[]{"1", "2", "3"}); 
    DragDropEffects effects = DragDrop.DoDragDrop(this.draggingElement, data, DragDropEffects.Move); 
    //... 
} 

private void PictureBox_Drop(object sender, DragEventArgs e) 
{ 
    string[] files = (string[])e.Data.GetData("Object"); 
    //... 
}