WPF-17行为(以控件在界面拖动为例)

行为并不是WPF中的核心的部分,是Expression Blend的设计特性。使用行为的地方,也是可以使用触发器取代的。不过行为使用起来也是有趣的,下面以一个简单的例子看看它的用法。
重写OnAttached()和OnDetaching()方法。通过AssociatedObject访问放置行为的元素。在事件中完成鼠标拖动控件的一些操作。如下:
 public class MyBehavior : Behavior
    {
        private Canvas canvas;


        private bool isDragging = false;


        private Point mouseOffset;


        protected override void OnAttached()
        {
            base.OnAttached();


            this.AssociatedObject.MouseLeftButtonDown += 
                
                new System.Windows.Input.MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown);


            this.AssociatedObject.MouseMove += 
                
                new System.Windows.Input.MouseEventHandler(AssociatedObject_MouseMove);


            this.AssociatedObject.MouseLeftButtonUp += 
                
                new System.Windows.Input.MouseButtonEventHandler(AssociatedObject_MouseRightButtonUp);
        }


        protected override void OnDetaching()
        {
            base.OnDetaching();


            this.AssociatedObject.MouseLeftButtonDown -=
                
                new System.Windows.Input.MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown);


            this.AssociatedObject.MouseMove -=


                new System.Windows.Input.MouseEventHandler(AssociatedObject_MouseMove);


            this.AssociatedObject.MouseLeftButtonUp -=


                new System.Windows.Input.MouseButtonEventHandler(AssociatedObject_MouseRightButtonUp);
        }


        void AssociatedObject_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (isDragging)
            {
                AssociatedObject.ReleaseMouseCapture();


                isDragging = false;
            }
        }


        void AssociatedObject_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (isDragging)
            {
                Point point = e.GetPosition(canvas);


                AssociatedObject.SetValue(Canvas.TopProperty, point.Y - mouseOffset.Y);


                AssociatedObject.SetValue(Canvas.LeftProperty, point.X - mouseOffset.X);
            }
        }


        void AssociatedObject_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (this.canvas == null)
            {
                canvas = VisualTreeHelper.GetParent(this.AssociatedObject) as Canvas;
            }


            isDragging = true;


            mouseOffset = e.GetPosition(AssociatedObject);


            AssociatedObject.CaptureMouse();
        }
    }
最重要的是在界面上使用行为。
一般WPF开发的应该都安装的有Blend。(以默认安装路径为例)需要引用C:\Program Files\Microsoft SDKs\Expression\Blend 3\Interactivity\Libraries\WPF下的System.Windows.Interactivity.dll。使用如下:

    
        
            
            
            
            
            
                
                    
                
            
            
            
                
                    
                
            
        
    
这样就实现了,按住鼠标左键拖动界面上图标的效果了。
代码下载: http://download.csdn.net/detail/yysyangyangyangshan/5422719

你可能感兴趣的:(WPF)