WPF鼠标拖放功能(拖放图片,文本)
2012-09-23 23:30
218 查看
对于拖放操作有两个方面:源和目标。为了创建拖放源,需要在某个位置调用DragDrop.DoDragDrop()方法初始化拖放操作。此时确定拖动操作的源,搁置希望移动的内容,并指明充许什么样的拖放效果(复制,移动等)。
通常会在响应PreviewMouseDown或MouseDown事件时,调用DoDragDrop()方法。
而接收的元素需要将它的AllowDrop属性设置为true,还需要通过处理Drop事件来处理数据。
前台代码:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox Name="source" Grid.Row="0" Background="Purple" Foreground="White" MouseDown="source_MouseDown">博客园:www.cnblogs.com</TextBox>
<Image Source="http://static.cnblogs.com/images/logo_small.gif" Grid.Column="1" Stretch="None" MouseDown="Image_MouseDown"></Image>
<Label Name="target" Grid.Row="1" Background="YellowGreen" AllowDrop="True" Drop="OnDrop">文本拖到这里</Label>
<Image Name="targetImg" Grid.Row="1" Grid.Column="1" AllowDrop="True" Drop="targetImg_Drop_1" Stretch="None" Source="http://www.baidu.com/img/baidu_sylogo1.gif"></Image>
</Grid>
后台代码:
/// <summary>
/// 文本源数据
/// </summary>
private void source_MouseDown(object sender, MouseButtonEventArgs e)
{
TextBox objText = sender as TextBox;
DragDrop.DoDragDrop(objText, objText, DragDropEffects.Copy);
}
/// <summary>
/// 图片源数据
/// </summary>
private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
Image objImage = sender as Image;
DragDrop.DoDragDrop(objImage, objImage.Source, DragDropEffects.Copy);
}
/// <summary>
/// 目标位置
/// </summary>
private void OnDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effects = DragDropEffects.Copy;
else
return;
this.target.Content = e.Data.GetData(DataFormats.Text);
}
private void targetImg_Drop_1(object sender, DragEventArgs e)
{
e.Data.GetFormats();
if (e.Data.GetDataPresent("System.Windows.Media.Imaging.BitmapFrameDecode"))
e.Effects = DragDropEffects.Copy;
else
{
return;
}
// targetImg.Source = (ImageSource)e.Data.GetData("System.Windows.Media.Imaging.BitmapFrameDecode");
((Image)sender).Source = (ImageSource)e.Data.GetData("System.Windows.Media.Imaging.BitmapFrameDecode");
}
如果不知道拖放源数据是什么数据类型,可以使用实现了IDataObject接口的GetFormats()方法。如: e.Data.GetFormats(); 其中Data就实现了IDataObject接口。
通常会在响应PreviewMouseDown或MouseDown事件时,调用DoDragDrop()方法。
而接收的元素需要将它的AllowDrop属性设置为true,还需要通过处理Drop事件来处理数据。
前台代码:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox Name="source" Grid.Row="0" Background="Purple" Foreground="White" MouseDown="source_MouseDown">博客园:www.cnblogs.com</TextBox>
<Image Source="http://static.cnblogs.com/images/logo_small.gif" Grid.Column="1" Stretch="None" MouseDown="Image_MouseDown"></Image>
<Label Name="target" Grid.Row="1" Background="YellowGreen" AllowDrop="True" Drop="OnDrop">文本拖到这里</Label>
<Image Name="targetImg" Grid.Row="1" Grid.Column="1" AllowDrop="True" Drop="targetImg_Drop_1" Stretch="None" Source="http://www.baidu.com/img/baidu_sylogo1.gif"></Image>
</Grid>
后台代码:
/// <summary>
/// 文本源数据
/// </summary>
private void source_MouseDown(object sender, MouseButtonEventArgs e)
{
TextBox objText = sender as TextBox;
DragDrop.DoDragDrop(objText, objText, DragDropEffects.Copy);
}
/// <summary>
/// 图片源数据
/// </summary>
private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
Image objImage = sender as Image;
DragDrop.DoDragDrop(objImage, objImage.Source, DragDropEffects.Copy);
}
/// <summary>
/// 目标位置
/// </summary>
private void OnDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effects = DragDropEffects.Copy;
else
return;
this.target.Content = e.Data.GetData(DataFormats.Text);
}
private void targetImg_Drop_1(object sender, DragEventArgs e)
{
e.Data.GetFormats();
if (e.Data.GetDataPresent("System.Windows.Media.Imaging.BitmapFrameDecode"))
e.Effects = DragDropEffects.Copy;
else
{
return;
}
// targetImg.Source = (ImageSource)e.Data.GetData("System.Windows.Media.Imaging.BitmapFrameDecode");
((Image)sender).Source = (ImageSource)e.Data.GetData("System.Windows.Media.Imaging.BitmapFrameDecode");
}
如果不知道拖放源数据是什么数据类型,可以使用实现了IDataObject接口的GetFormats()方法。如: e.Data.GetFormats(); 其中Data就实现了IDataObject接口。
相关文章推荐
- c# WPF 中图片缩放功能,鼠标拖动位置
- 如何设置secureCRT的鼠标右键为弹出文本操作菜单功能
- WPF通过鼠标滑轮缩放显示图片
- 鼠标移到向上或向下的图片上,文本自动滚动
- 实现:为自写的表格控件中的表格线 实现鼠标拖放功能
- 用HTML5实现鼠标滚轮事件放大缩小图片的功能
- WPF通过鼠标滑轮缩放显示图片
- WPF(二)路由事件5.鼠标拖放
- 鼠标在文本上移动时层的显示与消失(以及层里显示图片)ASP.NET
- WPF拖放功能实现
- 鼠标在按钮上移动、按下或者弹起时显示按钮功能提示文本
- WPF Image鼠标进入后改变图片
- jquery动画 -- 6.制作带鼠标和键盘引导功能的图片浏览器
- WPF通过鼠标滑轮缩放显示图片
- 在jsp页面中添加富文本编译器(ueditor)+ 图片上传功能
- 在jsp页面中添加富文本编译器(ueditor)+ 图片上传功能
- jquery实现:提示文本/图片跟随鼠标移动
- 艾伟_转载:在WPF里面实现以鼠标位置为中心缩放移动图片
- C#(WPF)按钮图片不显示,壁纸切换功能
- js实现跟随鼠标移动且带关闭功能的图片广告实例