对于WPF里的dragDrop事件。是不是只能存在一个。例如:从webBwoser的页面里拖了一张图片到ImageCanvas里,再移动图片。会报异常
<Window x:Class="DragDropImage.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" AllowDrop="True" DragEnter="Window_DragEnter" Drop="Window_Drop"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style TargetType="Image">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="Image_PreviewMouseLeftButtonDown"/>
</Style>
</Window.Resources>
<Grid>
<Canvas Name="outerCanvas">
<Canvas Name="ImageCanvas" DragOver="ImageCanvas_DragOver" AllowDrop="True"
PreviewMouseLeftButtonUp="ImageCanvas_PreviewMouseLeftButtonUp"
MouseMove="ImageCanvas_MouseMove">
<Image Canvas.Left="120" Height="81" Width="60" Source="D:\Test c#\StringApplication\DragDropImage\images\Dimension32.png"/>
<Image Height="81" Width="60" Source="D:\Test c#\StringApplication\DragDropImage\images\CirclePillar32.png"/>
<Image Canvas.Left="180" Height="81" Width="60" Source="D:\Test c#\StringApplication\DragDropImage\images\Dimension32.png"/>
</Canvas>
</Canvas>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace DragDropImage
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private bool _isDragging = false;
private Point _startPoint;
private Point _relativePoint;
private Image _selectImage; public Window1()
{
InitializeComponent();
} private void Image_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_selectImage = sender as Image;
_startPoint = e.GetPosition(ImageCanvas);
_relativePoint = e.GetPosition(_selectImage);
_isDragging = true;
foreach (Image image in ImageCanvas.Children)
{
image.SetValue(Canvas.ZIndexProperty, 0);
}
_selectImage.SetValue(Canvas.ZIndexProperty, 1);
} private void ImageCanvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_isDragging = false;
} private void ImageCanvas_DragOver(object sender, DragEventArgs e)
{
if (e.Data != null && e.Data.GetDataPresent(typeof(Image)))
{
Image image = e.Data.GetData(typeof(Image)) as Image;
Point p = e.GetPosition(ImageCanvas); image.SetValue(Canvas.LeftProperty, p.X - _relativePoint.X);
image.SetValue(Canvas.TopProperty, p.Y - _relativePoint.Y);
}
} private void ImageCanvas_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed && _isDragging)
{
Point newPos = e.GetPosition(ImageCanvas);
if ((Math.Abs(newPos.X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance) ||
(Math.Abs(newPos.Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance))
{
ImageCanvas.GiveFeedback += new GiveFeedbackEventHandler(ImageCanvas_GiveFeedback);
DragDrop.DoDragDrop(ImageCanvas, _selectImage, DragDropEffects.Move);// 未将对象引用到对象实例
_isDragging = false;
ImageCanvas.GiveFeedback -= new GiveFeedbackEventHandler(ImageCanvas_GiveFeedback);
}
}
} void ImageCanvas_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
Mouse.SetCursor(Cursors.Arrow);
e.Handled = true;
} private void Window_DragEnter(object sender, DragEventArgs e)
{
e.Effects = DragDropEffects.Copy;
} private void Window_Drop(object sender, DragEventArgs e)
{
Product product = (Product)e.Data.GetData(typeof(Product));
Uri url = new Uri(product.Url);
BitmapImage bitmapImage = new BitmapImage(url);
Image image = new Image();
image.Source = bitmapImage;
} private void DragProductTo(string name,string url) {
Product product = new Product();
product.Url = url;
product.Name = name;
System.Windows.DragDrop.DoDragDrop(this,product,DragDropEffects.Copy);
}
}
}在ImageCanvas中可以移动图片,当鼠标释放的时候就会有异常:“未将对象引用到对象实例”?
<Window x:Class="DragDropImage.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" AllowDrop="True" DragEnter="Window_DragEnter" Drop="Window_Drop"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style TargetType="Image">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="Image_PreviewMouseLeftButtonDown"/>
</Style>
</Window.Resources>
<Grid>
<Canvas Name="outerCanvas">
<Canvas Name="ImageCanvas" DragOver="ImageCanvas_DragOver" AllowDrop="True"
PreviewMouseLeftButtonUp="ImageCanvas_PreviewMouseLeftButtonUp"
MouseMove="ImageCanvas_MouseMove">
<Image Canvas.Left="120" Height="81" Width="60" Source="D:\Test c#\StringApplication\DragDropImage\images\Dimension32.png"/>
<Image Height="81" Width="60" Source="D:\Test c#\StringApplication\DragDropImage\images\CirclePillar32.png"/>
<Image Canvas.Left="180" Height="81" Width="60" Source="D:\Test c#\StringApplication\DragDropImage\images\Dimension32.png"/>
</Canvas>
</Canvas>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace DragDropImage
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private bool _isDragging = false;
private Point _startPoint;
private Point _relativePoint;
private Image _selectImage; public Window1()
{
InitializeComponent();
} private void Image_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_selectImage = sender as Image;
_startPoint = e.GetPosition(ImageCanvas);
_relativePoint = e.GetPosition(_selectImage);
_isDragging = true;
foreach (Image image in ImageCanvas.Children)
{
image.SetValue(Canvas.ZIndexProperty, 0);
}
_selectImage.SetValue(Canvas.ZIndexProperty, 1);
} private void ImageCanvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_isDragging = false;
} private void ImageCanvas_DragOver(object sender, DragEventArgs e)
{
if (e.Data != null && e.Data.GetDataPresent(typeof(Image)))
{
Image image = e.Data.GetData(typeof(Image)) as Image;
Point p = e.GetPosition(ImageCanvas); image.SetValue(Canvas.LeftProperty, p.X - _relativePoint.X);
image.SetValue(Canvas.TopProperty, p.Y - _relativePoint.Y);
}
} private void ImageCanvas_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed && _isDragging)
{
Point newPos = e.GetPosition(ImageCanvas);
if ((Math.Abs(newPos.X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance) ||
(Math.Abs(newPos.Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance))
{
ImageCanvas.GiveFeedback += new GiveFeedbackEventHandler(ImageCanvas_GiveFeedback);
DragDrop.DoDragDrop(ImageCanvas, _selectImage, DragDropEffects.Move);// 未将对象引用到对象实例
_isDragging = false;
ImageCanvas.GiveFeedback -= new GiveFeedbackEventHandler(ImageCanvas_GiveFeedback);
}
}
} void ImageCanvas_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
Mouse.SetCursor(Cursors.Arrow);
e.Handled = true;
} private void Window_DragEnter(object sender, DragEventArgs e)
{
e.Effects = DragDropEffects.Copy;
} private void Window_Drop(object sender, DragEventArgs e)
{
Product product = (Product)e.Data.GetData(typeof(Product));
Uri url = new Uri(product.Url);
BitmapImage bitmapImage = new BitmapImage(url);
Image image = new Image();
image.Source = bitmapImage;
} private void DragProductTo(string name,string url) {
Product product = new Product();
product.Url = url;
product.Name = name;
System.Windows.DragDrop.DoDragDrop(this,product,DragDropEffects.Copy);
}
}
}在ImageCanvas中可以移动图片,当鼠标释放的时候就会有异常:“未将对象引用到对象实例”?
解决方案 »
- winform 怎么发送文件给GPRS模块
- 大家来看看,我这样用new定义正则字段作为标签合理吗?会不会浪费内存开销?
- 弹出新窗口关闭旧窗口
- 聪明的.net程序员工具 CoolClass 1.2 将免费进行到底 无任何限制版
- HtmlElement的对象的OuterHtml属性问题
- 为何在网络传输的过程中,不能像Progressbar一样,时时刷新label控件?我想把传输的数据反映到Label控件上!!
- CS0006: 未能找到元数据文件 是什么意思?
- 学习copyhook,出现错误LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
- 讀取XML配置文件
- 请问~~~~~
- 动态生成ajax控件问题
- 速度慢,求助优化。
private void Window_Drop(object sender, DragEventArgs e)
{
Product product = (Product)e.Data.GetData(typeof(Product));
if (product != null)//该值可能为空吧....
{
Uri url = new Uri(product.Url);
BitmapImage bitmapImage = new BitmapImage(url);
Image image = new Image();
image.Source = bitmapImage;
}
try{
DragDrop.DoDragDrop(ImageCanvas, _selectImage, DragDropEffects.Move);
}catch{
//throw new NullReferenceException();
return;
}
当捕获到空指针时,就被return。异常捕获到,图片可以任意拖动了,不过return掉不是最终的解决办法,还请大家多多帮忙。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace DragDropImage
{
public class Product
{
string url;
string name;
public string Url {
get { return this.url; }
set { this.url = value; }
} public string Name
{
get { return this.name; }
set { this.name = value; }
}
}
}
这一行运行完以后,product为null,
然后调用Uri url = new Uri(product.Url);就抛异常了
为了测试Product是null,我改了window1.xmal.cs里Window_Drop方法。
private void Window_Drop(object sender, DragEventArgs e)
{
Product product = new Product();
product.Url = "D:\\Test c#\\StringApplication\\DragDropImage\\images\\CirclePillar32.png";
product.Name = "simpleProduct";
Uri url = new Uri(product.Url);
BitmapImage bitmapImage = new BitmapImage(url);
Image image = new Image();
image.Source = bitmapImage;
} 注:如果父类有Drop事件,像window1有drop事件,而子节点ImageCanvas也有必须要有Drop事件(就算它什麽事也不做也要有Drop)。子类,父类的Drop事件要独立开来。