[英]Drag and Drop In WPF C#
以下是我在wpf中拖放的代码。 我在windowform中使用了相同的代码来拖放图像,但是我似乎不适用于wpf,您能帮上忙吗?
<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);
}
我认为此代码不正确:
if (e.Data.GetDataPresent(DataFormats.Bitmap, false) == true)
e.Effects = DragDropEffects.None;
检查== false。
另外,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");
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.