在本实例中我们将从ListBox中拖出一个图标到另外一个ListBox中,这是一个比较有用的小功能,在这里我们首先来看运行效果(点击下面的图片即可拖动左边的图标到右边的ListBox中去)。

实现过程是:1、我们把这个过程分为鼠标左键点击到左边的图标,使用Image_MouseLeftButtonDown事件。(设置一个标识符,标示当前已经被鼠标点击下去)

          2、点击到这个图标不放,在整个Canvas内移动,使用LayoutRoot_MouseMove事件(此时设置一个图标跟随鼠标的位置移动,这个图标既是被点击的图标)。

          3、当鼠标移动到右边的ListBox范围之内时放开鼠标左键,使用LayoutRoot_MouseLeftButtonUp事件(当鼠标左键弹起的时 候,判断标识符是否为true,如果是的话表示有图标被拖动,并且判断此时的鼠标位置是否在右边的ListBox范围之内,如果是则将被拖动的图标放入右 边的ListBox中) 

首先我们添加8张图片素材,然后编写一个类来获取图片列表,其CS代码如下:

  1. public class IphoneIco
  2. {
  3. #region 字段
  4. string icoName;
  5. BitmapImage icoImage;
  6. #endregion
  7. #region 属性
  8. /// <summary>
  9. /// 图标名称
  10. /// </summary>
  11. public string IcoName
  12. {
  13. get { return icoName; }
  14. set { icoName = value; }
  15. }
  16. /// <summary>
  17. /// 图标图像
  18. /// </summary>
  19. public BitmapImage IcoImage
  20. {
  21. get { return icoImage; }
  22. set { icoImage = value; }
  23. }
  24. #endregion
  25. #region 单一实例
  26. public static readonly IphoneIco instance = new IphoneIco();
  27. #endregion
  28. #region 公共方法
  29. public List<IphoneIco> getIphoneIcoList()
  30. {
  31. List<IphoneIco> iphoneIcoList = new List<IphoneIco>()
  32. {
  33. new IphoneIco(){ IcoName="1", IcoImage=new BitmapImage(new Uri("/SL5Drag;component/Images/1.png",UriKind.RelativeOrAbsolute))},
  34. new IphoneIco(){ IcoName="2", IcoImage=new BitmapImage(new Uri("/SL5Drag;component/Images/2.png",UriKind.RelativeOrAbsolute))},
  35. new IphoneIco(){ IcoName="3", IcoImage=new BitmapImage(new Uri("/SL5Drag;component/Images/3.png",UriKind.RelativeOrAbsolute))},
  36. new IphoneIco(){ IcoName="4", IcoImage=new BitmapImage(new Uri("/SL5Drag;component/Images/4.png",UriKind.RelativeOrAbsolute))},
  37. new IphoneIco(){ IcoName="5", IcoImage=new BitmapImage(new Uri("/SL5Drag;component/Images/5.png",UriKind.RelativeOrAbsolute))},
  38. new IphoneIco(){ IcoName="6", IcoImage=new BitmapImage(new Uri("/SL5Drag;component/Images/6.png",UriKind.RelativeOrAbsolute))},
  39. new IphoneIco(){ IcoName="7", IcoImage=new BitmapImage(new Uri("/SL5Drag;component/Images/7.png",UriKind.RelativeOrAbsolute))},
  40. new IphoneIco(){ IcoName="8", IcoImage=new BitmapImage(new Uri("/SL5Drag;component/Images/8.png",UriKind.RelativeOrAbsolute))}
  41. };
  42. return iphoneIcoList;
  43. }
  44. #endregion
  45. }

然后我们来看MainPage.xaml中的代码,向其中添加两个ListBox以及Image图标,其代码如下:

  1. <UserControl x:Class="SL5Drag.MainPage"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. mc:Ignorable="d"
  7. d:DesignHeight="400" d:DesignWidth="500">
  8. <!--这个Canvas中有鼠标左键弹起事件,以及鼠标移动事件-->
  9. <Canvas x:Name="LayoutRoot" AllowDrop="True"
  10. MouseLeftButtonUp="LayoutRoot_MouseLeftButtonUp"
  11. MouseMove="LayoutRoot_MouseMove">
  12. <!--这个是右边的ListBox,鼠标拖动的图标在这个ListBox范围内放开-->
  13. <ListBox Margin="400 0 0 0" Background="AliceBlue" Height="400"
  14. HorizontalAlignment="Right" Name="listBox2"
  15. VerticalAlignment="Top" Width="50" >
  16. <ListBox.ItemTemplate>
  17. <DataTemplate>
  18. <StackPanel Width="40" Height="40">
  19. <Border BorderThickness="1">
  20. <Image Source="{Binding IcoImage}" Width="30"
  21. Height="30" Margin="0,5,6,0"
  22. Tag="{Binding IcoName}"
  23. HorizontalAlignment="Center" />
  24. </Border>
  25. </StackPanel>
  26. </DataTemplate>
  27. </ListBox.ItemTemplate>
  28. </ListBox>
  29. <!--这个是左边的ListBox,鼠标将从此ListBox拖出图标-->
  30. <ListBox Name="listBox1" Background="AliceBlue" Width="50"
  31. HorizontalAlignment="Left" VerticalAlignment="Top"
  32. Height="400" >
  33. <ListBox.ItemTemplate>
  34. <DataTemplate>
  35. <StackPanel Width="40" Height="40">
  36. <Border BorderThickness="1">
  37. <Image Source="{Binding IcoImage}" Width="30"
  38. Height="30" Margin="0,5,6,0"
  39. Tag="{Binding IcoName}"
  40. MouseLeftButtonDown="Image_MouseLeftButtonDown"
  41. HorizontalAlignment="Center" />
  42. </Border>
  43. </StackPanel>
  44. </DataTemplate>
  45. </ListBox.ItemTemplate>
  46. </ListBox>
  47. <!--这个在鼠标拖动过程中显示的图标-->
  48. <Image Name="Img" Opacity="0.5" Width="30" Height="30"
  49. Margin="0,5,6,0" Visibility="Collapsed" HorizontalAlignment="Center" />
  50. </Canvas>
  51. </UserControl>

最后我们来看MainPage.xaml.cs代码,其中有相关的逻辑控制在注释中已注明:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. using System.Windows.Interactivity;
  13. using Microsoft.Expression.Interactivity;
  14. using Microsoft.Expression.Interactivity.Layout;
  15. using System.Windows.Media.Imaging;
  16. using System.ComponentModel;
  17. namespace SL5Drag
  18. {
  19. public partial class MainPage : UserControl
  20. {
  21. public MainPage()
  22. {
  23. InitializeComponent();
  24. //设置左边的ListBox显示的内容项
  25. this.listBox1.ItemsSource = IphoneIco.instance.getIphoneIcoList();
  26. string s = string.Empty;
  27. }
  28. List<IphoneIco> iphoneList;
  29. /// <summary>
  30. /// 标示是否按下鼠标左键
  31. /// </summary>
  32. bool leftMouseflag = false;
  33. /// <summary>
  34. /// 右边ListBox的结果集合
  35. /// </summary>
  36. private static List<IphoneIco> AddiphoneList = new List<IphoneIco>();
  37. /// <summary>
  38. /// 左边ListBox的结果集合
  39. /// </summary>
  40. public List<IphoneIco> IphoneList
  41. {
  42. get { return iphoneList; }
  43. set { iphoneList = value; }
  44. }
  45. private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  46. {
  47. //鼠标在ListBox中按下准备拖出图片的时候,设置leftMouseflag为true,并且设置Image为可见
  48. leftMouseflag = true;
  49. Image p_w_picpath=sender as Image;
  50. this.Img.Source = p_w_picpath.Source;
  51. Point point = e.GetPosition(null);
  52. this.Img.SetValue(Canvas.LeftProperty, point.X );
  53. this.Img.SetValue(Canvas.TopProperty, point.Y - 5.0);
  54. this.Img.Visibility = Visibility.Visible;
  55. }
  56. private void LayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  57. {
  58. //如果得知鼠标左键松动的位置是右边的ListBox上时则为右边的ListBox添加一列
  59. Point point =e.GetPosition(null);
  60. if (point.X > 400 && point.X < 450 && point.Y < 400&& leftMouseflag == true )
  61. {
  62. BitmapImage bimg = this.Img.Source as BitmapImage;
  63. this.Img.Visibility = Visibility.Collapsed;
  64. AddiphoneList.Add(new IphoneIco()
  65. {
  66. IcoName = "2",
  67. IcoImage = bimg
  68. });
  69. this.listBox2.ItemsSource = null;
  70. this.listBox2.ItemsSource = AddiphoneList;
  71. }
  72. else
  73. {
  74. this.Img.Visibility = Visibility.Collapsed;
  75. this.Img.Source = null;
  76. }
  77. leftMouseflag = false;
  78. }
  79. private void LayoutRoot_MouseMove(object sender, MouseEventArgs e)
  80. {
  81. //让图片跟随鼠标的移动而移动
  82. Point point = e.GetPosition(null);
  83. this.Img.SetValue(Canvas.LeftProperty, point.X);
  84. this.Img.SetValue(Canvas.TopProperty, point.Y-5.0);
  85. }
  86. }
  87. }

本实例采用VS2010+Silverlight 4.0编写,如需源码请点击 SL4Drag.zip 下载。

转载于:https://blog.51cto.com/chengxingliang/826456

Silverlight实用窍门系列:43.Silverlight从ListBox拖拽图标到另一ListBox相关推荐

  1. Silverlight实用窍门大集合+Silverlight 5 最全新特性【目录索引】

    在最近的几个月内整理出了Silverlight的一些相关的比较实用的功能讲解文章,并且随着Silverlight 5 beta版本的发布整理出的新特性系列文章,在这里做一个总的概括和索引,以方便大家观 ...

  2. Silverlight实用窍门系列:71.Silverlight的Style

    此文章实例基于Silverlight实用窍门系列:68.Silverlight的资源字典ResourceDictionary,如有数据源疑问请参考该文章. 在Silverlight中的Style相当于 ...

  3. Silverlight实用窍门系列:59.多个中心点联动多线的可拖动控件扩展为拓扑图

    在本系列的第17篇文章中"Silverlight实用窍门系列:17.中心点联动多线的可拖动控件(绘制工程图.拓扑图基础) ",制作了基本的中心联动图标.有园友对此图的扩展不是很清晰 ...

  4. Silverlight实用窍门系列:65.Silverlight的数据模板DataTemplate(一)使用数据模板

    在Silverlight中,我们的数据列表显示控件有ListBox.DataGrid等.对于显示出来的数据条目样式外观的管理是通过DataTemplate(数据模板)来完成的.本节将从DataTemp ...

  5. Silverlight实用窍门系列:66.Silverlight的数据模板DataTemplate(二)获取数据模板控件...

    在Silverlight中我们使用了数据模板,在很多时候也需要获取到数据模板上的控件,并且进行改变.本文使用的实例数据源和上节相同,如需了解请下载源码查看. 当然在这里我们可以分为获取数据模板原始控件 ...

  6. Silverlight实用窍门系列:42.读取拖动到控件上的外部txt和jpg文件,多外部文件的拖动【附带实例源码】...

    本实例将读取拖动到Silverlight的ListBox控件中的txt文件或者Jpg文件.在本实例中将讲如果通过UIelementA.Drop事件获取到拖动到UIelementA上的文件的相关名称以及 ...

  7. Silverlight实用窍门系列:58.Silverlight中的Binding使用(三)-数据集合绑定

      在本文中将以ListBox为例讲述在Silverlight中Binding数据集合. 在这里我们将实体集的绑定分为三类:    一.直接控件绑定.    二.DataTemplate模板绑定.   ...

  8. Silverlight实用窍门系列:40.Silverlight中捕捉视频,截图保存到本地

    在Silverlight中我们可以捕捉视频设备以制作视频会议系统,或者通过视频设备截图功能上传头像等功能. 下面我们通过一个简单的实例来访问视频设备,并且截取图像下载该截图文件至本地. 一.在Silv ...

  9. Silverlight实用窍门系列:61.Silverlight中的Trigger触发器,自定义翻页触发器

    在Silverlight应用程序和客户进行交互工作的时候可以不用写后台代码而通过Xaml代码来实现,在本文我们将学习了解Trigger触发器. Trigger触发器:引发动作的因素,比如鼠标点击.键盘 ...

  10. Silverlight实用窍门系列:35.细解Silverlight冒泡路由事件和注册冒泡路由事件【附带实例源码】...

    Silverlight中的事件分为普通事件和冒泡路由事件,它并没有包括WPF中的隧道路由事件,在本章中将详细讲解冒泡路由事件和如何注册一个冒泡路由事件. 一.细解冒泡路由事件 冒泡路由事件可以比喻为: ...

最新文章

  1. 干货丨一份不可多得的深度学习技巧指南
  2. 关于 Android 进程保活,你所需要知道的一切
  3. 13. sizeof 和 strlen 的区别
  4. Linux系统中磁盘创建管理(一)
  5. 福昕风腾pdf导出为html,福昕风腾PDF套件快速指引.pdf
  6. 开源软件冲破云霄,“机智号”直升机首飞成功,还带来了第一个火星机场!...
  7. JS 中对象的简单创建和继承
  8. Unity3D(五)渲染管线
  9. 数据结构面试常见问题
  10. TD-SCDMA迫零块线性均衡
  11. PHP架构师成长路线,PHP架构师要求
  12. linux mac 字体,Mac 和 Windows 的中文字体显示效果
  13. android 发送短信例子
  14. linux分区变为空闲,分析linux系统中磁盘空闲空间的管理方法
  15. 【网络】吐血整理-Java网络合集
  16. C#秘密武器之多线程——参数与返回值
  17. 【2020-07】字节跳动面试凉经(年轻人的第一场 技术面试)
  18. java 是什么_java中是什么意思?
  19. ITUNES更改备份保存路径(不放C盘,不占C盘)
  20. python 连接mysql报错:mysql.connector.errors.NotSupportedError: Authentication plugin ‘caching_sha2_passw

热门文章

  1. linux:云端 ubuntu下挂载数据盘
  2. 关于私募基金高水位业绩报酬计提的N种方式!
  3. 【目标定位】基于matlab去偏卡尔曼滤波目标定位仿真【含Matlab源码 140期】
  4. 【图像评价】基于matlab GUI图像质量评价【含Matlab源码 1373期】
  5. 【数字信号调制】基于matlab GUI ASK+OOK+BPSK+8PSK+QPSK+AM调制解调【含Matlab源码 1368期】
  6. 【细胞分割】基于matlab GUI阙值+边缘+形态学+种子点图像分割【含Matlab源码 615期】
  7. oracle 最大一行,一行最大column数和row piece-概念
  8. anaconda开发python_使用anaconda和pycharm搭建多python本版的开发环境
  9. 逻辑运算 神经网络_使用神经网络实现逻辑门(第2部分)
  10. ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问。