Silverlight实用窍门系列:43.Silverlight从ListBox拖拽图标到另一ListBox
在本实例中我们将从ListBox中拖出一个图标到另外一个ListBox中,这是一个比较有用的小功能,在这里我们首先来看运行效果(点击下面的图片即可拖动左边的图标到右边的ListBox中去)。
实现过程是:1、我们把这个过程分为鼠标左键点击到左边的图标,使用Image_MouseLeftButtonDown事件。(设置一个标识符,标示当前已经被鼠标点击下去)
2、点击到这个图标不放,在整个Canvas内移动,使用LayoutRoot_MouseMove事件(此时设置一个图标跟随鼠标的位置移动,这个图标既是被点击的图标)。
3、当鼠标移动到右边的ListBox范围之内时放开鼠标左键,使用LayoutRoot_MouseLeftButtonUp事件(当鼠标左键弹起的时 候,判断标识符是否为true,如果是的话表示有图标被拖动,并且判断此时的鼠标位置是否在右边的ListBox范围之内,如果是则将被拖动的图标放入右 边的ListBox中)
首先我们添加8张图片素材,然后编写一个类来获取图片列表,其CS代码如下:
- public class IphoneIco
- {
- #region 字段
- string icoName;
- BitmapImage icoImage;
- #endregion
- #region 属性
- /// <summary>
- /// 图标名称
- /// </summary>
- public string IcoName
- {
- get { return icoName; }
- set { icoName = value; }
- }
- /// <summary>
- /// 图标图像
- /// </summary>
- public BitmapImage IcoImage
- {
- get { return icoImage; }
- set { icoImage = value; }
- }
- #endregion
- #region 单一实例
- public static readonly IphoneIco instance = new IphoneIco();
- #endregion
- #region 公共方法
- public List<IphoneIco> getIphoneIcoList()
- {
- List<IphoneIco> iphoneIcoList = new List<IphoneIco>()
- {
- new IphoneIco(){ IcoName="1", IcoImage=new BitmapImage(new Uri("/SL5Drag;component/Images/1.png",UriKind.RelativeOrAbsolute))},
- new IphoneIco(){ IcoName="2", IcoImage=new BitmapImage(new Uri("/SL5Drag;component/Images/2.png",UriKind.RelativeOrAbsolute))},
- new IphoneIco(){ IcoName="3", IcoImage=new BitmapImage(new Uri("/SL5Drag;component/Images/3.png",UriKind.RelativeOrAbsolute))},
- new IphoneIco(){ IcoName="4", IcoImage=new BitmapImage(new Uri("/SL5Drag;component/Images/4.png",UriKind.RelativeOrAbsolute))},
- new IphoneIco(){ IcoName="5", IcoImage=new BitmapImage(new Uri("/SL5Drag;component/Images/5.png",UriKind.RelativeOrAbsolute))},
- new IphoneIco(){ IcoName="6", IcoImage=new BitmapImage(new Uri("/SL5Drag;component/Images/6.png",UriKind.RelativeOrAbsolute))},
- new IphoneIco(){ IcoName="7", IcoImage=new BitmapImage(new Uri("/SL5Drag;component/Images/7.png",UriKind.RelativeOrAbsolute))},
- new IphoneIco(){ IcoName="8", IcoImage=new BitmapImage(new Uri("/SL5Drag;component/Images/8.png",UriKind.RelativeOrAbsolute))}
- };
- return iphoneIcoList;
- }
- #endregion
- }
然后我们来看MainPage.xaml中的代码,向其中添加两个ListBox以及Image图标,其代码如下:
- <UserControl x:Class="SL5Drag.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- d:DesignHeight="400" d:DesignWidth="500">
- <!--这个Canvas中有鼠标左键弹起事件,以及鼠标移动事件-->
- <Canvas x:Name="LayoutRoot" AllowDrop="True"
- MouseLeftButtonUp="LayoutRoot_MouseLeftButtonUp"
- MouseMove="LayoutRoot_MouseMove">
- <!--这个是右边的ListBox,鼠标拖动的图标在这个ListBox范围内放开-->
- <ListBox Margin="400 0 0 0" Background="AliceBlue" Height="400"
- HorizontalAlignment="Right" Name="listBox2"
- VerticalAlignment="Top" Width="50" >
- <ListBox.ItemTemplate>
- <DataTemplate>
- <StackPanel Width="40" Height="40">
- <Border BorderThickness="1">
- <Image Source="{Binding IcoImage}" Width="30"
- Height="30" Margin="0,5,6,0"
- Tag="{Binding IcoName}"
- HorizontalAlignment="Center" />
- </Border>
- </StackPanel>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
- <!--这个是左边的ListBox,鼠标将从此ListBox拖出图标-->
- <ListBox Name="listBox1" Background="AliceBlue" Width="50"
- HorizontalAlignment="Left" VerticalAlignment="Top"
- Height="400" >
- <ListBox.ItemTemplate>
- <DataTemplate>
- <StackPanel Width="40" Height="40">
- <Border BorderThickness="1">
- <Image Source="{Binding IcoImage}" Width="30"
- Height="30" Margin="0,5,6,0"
- Tag="{Binding IcoName}"
- MouseLeftButtonDown="Image_MouseLeftButtonDown"
- HorizontalAlignment="Center" />
- </Border>
- </StackPanel>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
- <!--这个在鼠标拖动过程中显示的图标-->
- <Image Name="Img" Opacity="0.5" Width="30" Height="30"
- Margin="0,5,6,0" Visibility="Collapsed" HorizontalAlignment="Center" />
- </Canvas>
- </UserControl>
最后我们来看MainPage.xaml.cs代码,其中有相关的逻辑控制在注释中已注明:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- using System.Windows.Interactivity;
- using Microsoft.Expression.Interactivity;
- using Microsoft.Expression.Interactivity.Layout;
- using System.Windows.Media.Imaging;
- using System.ComponentModel;
- namespace SL5Drag
- {
- public partial class MainPage : UserControl
- {
- public MainPage()
- {
- InitializeComponent();
- //设置左边的ListBox显示的内容项
- this.listBox1.ItemsSource = IphoneIco.instance.getIphoneIcoList();
- string s = string.Empty;
- }
- List<IphoneIco> iphoneList;
- /// <summary>
- /// 标示是否按下鼠标左键
- /// </summary>
- bool leftMouseflag = false;
- /// <summary>
- /// 右边ListBox的结果集合
- /// </summary>
- private static List<IphoneIco> AddiphoneList = new List<IphoneIco>();
- /// <summary>
- /// 左边ListBox的结果集合
- /// </summary>
- public List<IphoneIco> IphoneList
- {
- get { return iphoneList; }
- set { iphoneList = value; }
- }
- private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- //鼠标在ListBox中按下准备拖出图片的时候,设置leftMouseflag为true,并且设置Image为可见
- leftMouseflag = true;
- Image p_w_picpath=sender as Image;
- this.Img.Source = p_w_picpath.Source;
- Point point = e.GetPosition(null);
- this.Img.SetValue(Canvas.LeftProperty, point.X );
- this.Img.SetValue(Canvas.TopProperty, point.Y - 5.0);
- this.Img.Visibility = Visibility.Visible;
- }
- private void LayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- //如果得知鼠标左键松动的位置是右边的ListBox上时则为右边的ListBox添加一列
- Point point =e.GetPosition(null);
- if (point.X > 400 && point.X < 450 && point.Y < 400&& leftMouseflag == true )
- {
- BitmapImage bimg = this.Img.Source as BitmapImage;
- this.Img.Visibility = Visibility.Collapsed;
- AddiphoneList.Add(new IphoneIco()
- {
- IcoName = "2",
- IcoImage = bimg
- });
- this.listBox2.ItemsSource = null;
- this.listBox2.ItemsSource = AddiphoneList;
- }
- else
- {
- this.Img.Visibility = Visibility.Collapsed;
- this.Img.Source = null;
- }
- leftMouseflag = false;
- }
- private void LayoutRoot_MouseMove(object sender, MouseEventArgs e)
- {
- //让图片跟随鼠标的移动而移动
- Point point = e.GetPosition(null);
- this.Img.SetValue(Canvas.LeftProperty, point.X);
- this.Img.SetValue(Canvas.TopProperty, point.Y-5.0);
- }
- }
- }
本实例采用VS2010+Silverlight 4.0编写,如需源码请点击 SL4Drag.zip 下载。
转载于:https://blog.51cto.com/chengxingliang/826456
Silverlight实用窍门系列:43.Silverlight从ListBox拖拽图标到另一ListBox相关推荐
- Silverlight实用窍门大集合+Silverlight 5 最全新特性【目录索引】
在最近的几个月内整理出了Silverlight的一些相关的比较实用的功能讲解文章,并且随着Silverlight 5 beta版本的发布整理出的新特性系列文章,在这里做一个总的概括和索引,以方便大家观 ...
- Silverlight实用窍门系列:71.Silverlight的Style
此文章实例基于Silverlight实用窍门系列:68.Silverlight的资源字典ResourceDictionary,如有数据源疑问请参考该文章. 在Silverlight中的Style相当于 ...
- Silverlight实用窍门系列:59.多个中心点联动多线的可拖动控件扩展为拓扑图
在本系列的第17篇文章中"Silverlight实用窍门系列:17.中心点联动多线的可拖动控件(绘制工程图.拓扑图基础) ",制作了基本的中心联动图标.有园友对此图的扩展不是很清晰 ...
- Silverlight实用窍门系列:65.Silverlight的数据模板DataTemplate(一)使用数据模板
在Silverlight中,我们的数据列表显示控件有ListBox.DataGrid等.对于显示出来的数据条目样式外观的管理是通过DataTemplate(数据模板)来完成的.本节将从DataTemp ...
- Silverlight实用窍门系列:66.Silverlight的数据模板DataTemplate(二)获取数据模板控件...
在Silverlight中我们使用了数据模板,在很多时候也需要获取到数据模板上的控件,并且进行改变.本文使用的实例数据源和上节相同,如需了解请下载源码查看. 当然在这里我们可以分为获取数据模板原始控件 ...
- Silverlight实用窍门系列:42.读取拖动到控件上的外部txt和jpg文件,多外部文件的拖动【附带实例源码】...
本实例将读取拖动到Silverlight的ListBox控件中的txt文件或者Jpg文件.在本实例中将讲如果通过UIelementA.Drop事件获取到拖动到UIelementA上的文件的相关名称以及 ...
- Silverlight实用窍门系列:58.Silverlight中的Binding使用(三)-数据集合绑定
在本文中将以ListBox为例讲述在Silverlight中Binding数据集合. 在这里我们将实体集的绑定分为三类: 一.直接控件绑定. 二.DataTemplate模板绑定. ...
- Silverlight实用窍门系列:40.Silverlight中捕捉视频,截图保存到本地
在Silverlight中我们可以捕捉视频设备以制作视频会议系统,或者通过视频设备截图功能上传头像等功能. 下面我们通过一个简单的实例来访问视频设备,并且截取图像下载该截图文件至本地. 一.在Silv ...
- Silverlight实用窍门系列:61.Silverlight中的Trigger触发器,自定义翻页触发器
在Silverlight应用程序和客户进行交互工作的时候可以不用写后台代码而通过Xaml代码来实现,在本文我们将学习了解Trigger触发器. Trigger触发器:引发动作的因素,比如鼠标点击.键盘 ...
- Silverlight实用窍门系列:35.细解Silverlight冒泡路由事件和注册冒泡路由事件【附带实例源码】...
Silverlight中的事件分为普通事件和冒泡路由事件,它并没有包括WPF中的隧道路由事件,在本章中将详细讲解冒泡路由事件和如何注册一个冒泡路由事件. 一.细解冒泡路由事件 冒泡路由事件可以比喻为: ...
最新文章
- 干货丨一份不可多得的深度学习技巧指南
- 关于 Android 进程保活,你所需要知道的一切
- 13. sizeof 和 strlen 的区别
- Linux系统中磁盘创建管理(一)
- 福昕风腾pdf导出为html,福昕风腾PDF套件快速指引.pdf
- 开源软件冲破云霄,“机智号”直升机首飞成功,还带来了第一个火星机场!...
- JS 中对象的简单创建和继承
- Unity3D(五)渲染管线
- 数据结构面试常见问题
- TD-SCDMA迫零块线性均衡
- PHP架构师成长路线,PHP架构师要求
- linux mac 字体,Mac 和 Windows 的中文字体显示效果
- android 发送短信例子
- linux分区变为空闲,分析linux系统中磁盘空闲空间的管理方法
- 【网络】吐血整理-Java网络合集
- C#秘密武器之多线程——参数与返回值
- 【2020-07】字节跳动面试凉经(年轻人的第一场 技术面试)
- java 是什么_java中是什么意思?
- ITUNES更改备份保存路径(不放C盘,不占C盘)
- python 连接mysql报错:mysql.connector.errors.NotSupportedError: Authentication plugin ‘caching_sha2_passw
热门文章
- linux:云端 ubuntu下挂载数据盘
- 关于私募基金高水位业绩报酬计提的N种方式!
- 【目标定位】基于matlab去偏卡尔曼滤波目标定位仿真【含Matlab源码 140期】
- 【图像评价】基于matlab GUI图像质量评价【含Matlab源码 1373期】
- 【数字信号调制】基于matlab GUI ASK+OOK+BPSK+8PSK+QPSK+AM调制解调【含Matlab源码 1368期】
- 【细胞分割】基于matlab GUI阙值+边缘+形态学+种子点图像分割【含Matlab源码 615期】
- oracle 最大一行,一行最大column数和row piece-概念
- anaconda开发python_使用anaconda和pycharm搭建多python本版的开发环境
- 逻辑运算 神经网络_使用神经网络实现逻辑门(第2部分)
- ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问。