Silverlight拖放功能相关应用技巧分享

开发 开发工具
如果我们想要实现Silverlight拖放功能,首先需要做的就出发一些相关事件,这些事件包括:MouseLeftButtonDown事件;MouseMove事件;MouseLeftButtonUp事件等等。

Silverlight工具是一个非常有用的开发工具。可以帮助开发人员轻松实现基于多媒体播放操作等功能。在实际使用中,我们会体会到其强大的功能特定。在这里就先来了解其中Silverlight拖放功能的作用。#t#

在Silverlight拖放功能的实现中,分为三个步骤:

1.按下鼠标,触发MouseLeftButtonDown事件,选择要拖动的对象。

2.移动鼠标,触发MouseMove事件,移动选择的对象。

3.放开鼠标,触发MouseLeftButtonUp事件,停止捕捉事件。

做一个简单的界面,用一个按钮来显示Silverlight拖放功能,如下XAML声明:

  1. < Canvas Background="#46461F"> 
  2. < Button   
  3. MouseLeftButtonDown="OnMouseDown"   
  4. MouseMove="OnMouseMove" 
  5. MouseLeftButtonUp="OnMouseUp"   
  6. Canvas.Left="50" Canvas.Top="50"
     Background="Red" 
  7. FontSize="18" 
  8. Width="160" Height="80"> 
  9. < Button.Content> 
  10. < StackPanel Orientation=
    "Horizontal" HorizontalAlignment
    ="Center" 
  11. VerticalAlignment="Center"> 
  12. < Image Source="smile_6.png">< /Image> 
  13. < TextBlock Text="拖动我" 
    VerticalAlignment="Center"
     Margin="10">< /TextBlock> 
  14. < /StackPanel> 
  15. < /Button.Content> 
  16. < /Button> 
  17. < /Canvas> 

这里为了界面显示效果,使用了控件模板,后续会专门讲到。

Silverlight拖放功能之开始拖放操作

开始拖放操作,实现MouseLeftButtonDown事件处理程序,用两个全局变量来记录当前鼠标的位置和鼠标是否保持移动。

 

  1. bool trackingMouseMove = false;  
  2. Point mousePosition;  
  3. void OnMouseDown(object sender, 
    MouseButtonEventArgs e)  
  4. {  
  5. FrameworkElement element = sender 
    as FrameworkElement;  
  6. mousePosition = e.GetPosition(null);  
  7. trackingMouseMove = true;  
  8. if (null != element)  
  9. {  
  10. element.CaptureMouse();  
  11. element.Cursor = Cursors.Hand;  
  12. }  

Silverlight拖放功能之移动对象

移动对象,实现MouseMove事件处理程序,计算元素的位置并更新,同时更新鼠标的位置。

 

  1. void OnMouseMove(object sender, 
    MouseEventArgs e)  
  2. {  
  3. FrameworkElement element = 
    sender as FrameworkElement;  
  4. if (trackingMouseMove)  
  5. {  
  6. double deltaV = e.GetPosition(null).
    Y - mousePosition.Y;  
  7. double deltaH = e.GetPosition(null).
    X - mousePosition.X;  
  8. double newTop = deltaV + (double)
    element.GetValue(Canvas.TopProperty);  
  9. double newLeft = deltaH + (double)
    element.GetValue(Canvas.LeftProperty);  
  10. element.SetValue(Canvas.TopProperty, newTop);  
  11. element.SetValue(Canvas.LeftProperty, newLeft);  
  12. mousePosition = e.GetPosition(null);  
  13. }  

Silverlight拖放功能之完成拖放操作

完成拖放操作,实现MouseLeftButtonUp事件处理程序。

 

  1. void OnMouseUp(object sender, 
    MouseButtonEventArgs e)  
  2. {  
  3. FrameworkElement element = 
    sender as FrameworkElement;  
  4. trackingMouseMove = false;  
  5. element.ReleaseMouseCapture();  
  6. mousePositionmousePosition.X = 
    mousePosition
    .Y = 0;  
  7. element.Cursor = null;  

 

责任编辑:曹凯 来源: 博客园
相关推荐

2009-12-29 16:08:41

Silverlight

2009-12-29 17:56:47

Silverlight

2009-12-30 18:23:13

Silverlight

2009-12-30 13:37:24

Silverlight

2009-12-30 18:18:32

Silverlight

2009-12-31 17:00:40

Silverlight

2010-01-04 14:35:55

Silverlight

2009-12-31 16:44:53

Silverlight

2009-12-31 10:21:53

Silverlight

2009-12-30 09:55:51

Silverlight

2010-01-28 10:55:14

Android电源管理

2010-01-04 14:49:30

Silverlight

2010-02-05 13:44:06

C++ eof()函数

2010-03-04 14:39:52

Python读取输入值

2009-12-30 10:15:57

Silverlight

2009-12-30 16:19:49

Silverlight

2010-01-04 14:14:43

Silverlight

2009-12-10 17:27:39

PHP操作Cookie

2010-02-22 17:58:06

WCF异步上传

2010-01-25 18:33:35

Android键盘操作
点赞
收藏

51CTO技术栈公众号