今日您是第次访问
设为主页
添加改藏
网站优化
 
了解WPF技术
Silverlight WPF/E教程
WPF应用程序教程
当前位置:网站主页->Silverlight教程->文章浏览

Silverlight 2.0 控件自由拖拽

日期:2008-4-28   作者:天使坠

实例来自“天使坠”,由灵代写介绍之:

         主要核心使用.Net Silverlight SDK的类库中委托语言如(C#)与浏览器的交互之事件灵活使用,本实例适合入门的朋友了解Silverlight2.0事件的绑定方法,并且使用.Net中的属性机制的使用。并且灵活的点击状态的记录,让您更灵活的进入类库委托代码的使用方法:

using System;
using System.Collections.Generic;
using System.Linq;
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;

namespace DragDemo
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }
        bool isClick = false;
        Point before = new Point();
        double x_b;
        double y_b;
        private void btnTest_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            isClick = true;
      //      before = e.GetPosition(null);
            x_b = e.GetPosition(this.layMove).X;
            y_b = e.GetPosition(this.layMove).Y;
            double x = (double)this.layTest.GetValue(Canvas.LeftProperty);
            double y = (double)this.layTest.GetValue(Canvas.TopProperty);
            this.btnTest.Content = x_b + "," + y_b ;
                this.txtTest.Text=x+","+y;
        }

        private void btnTest_MouseMove(object sender, MouseEventArgs e)
        {
            if (isClick)
            {
                Canvas t = this.layMove;
                Point point = e.GetPosition(t);
                double x = (double)this.layTest.GetValue(Canvas.LeftProperty);
                double y = (double)this.layTest.GetValue(Canvas.TopProperty);
                this.btnTest.Content = x + "," + y + " " + this.isClick;
                double x1 = point.X - x_b;
                double y1 = point.Y - y_b; ;
                x_b = point.X;
                y_b = point.Y;
                this.layTest.SetValue(Canvas.LeftProperty,x+ x1);
                this.layTest.SetValue(Canvas.TopProperty,y+ y1);
                this.btnTest.Content = point.X + "," + point.Y ;
                this.txtTest.Text = x + "," + y + " " + (  x_b) + "," + (  y_b);
            }
        }

        private void btnTest_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isClick = false;
            double x = (double)this.layTest.GetValue(Canvas.LeftProperty);
            double y = (double)this.layTest.GetValue(Canvas.TopProperty);
            this.btnTest.Content = e.GetPosition(this.layMove).X + "," + e.GetPosition(this.layMove).Y;
            this.txtTest.Text = x + "," + y ;
        }

        private void layTest_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }
    }
}
演示效果: