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

行为并不是WPF中的核心的部分,是Expression Blend的设计特性。使用行为的地方,也是可以使用触发器取代的。不过行为使用起来也是有趣的,下面以一个简单的例子看看它的用法。
重写OnAttached()和OnDetaching()方法。通过AssociatedObject访问放置行为的元素。在事件中完成鼠标拖动控件的一些操作。如下:
 public class MyBehavior : Behavior<UIElement>
    {
        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。使用如下:
<Window x:Class="TestBehavior.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ibehavior="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:local="clr-namespace:TestBehavior"
        Title="MainWindow" Height="466" Width="875">
    <Grid>
        <Canvas>
            <!--没有使用行为-->
            <Rectangle Canvas.Left="10" Canvas.Top="10" Fill="Yellow" Width="40" Height="60">
            </Rectangle>
            <!--使用行为-->
            <Ellipse Canvas.Left="10" Canvas.Top="70" Fill="Blue" Width="80" Height="60">
                <ibehavior:Interaction.Behaviors>
                    <local:MyBehavior></local:MyBehavior>
                </ibehavior:Interaction.Behaviors>
            </Ellipse>
            <!--使用行为-->
            <Ellipse Canvas.Left="80" Canvas.Top="70" Fill="OrangeRed" Width="40" Height="70">
                <ibehavior:Interaction.Behaviors>
                    <local:MyBehavior></local:MyBehavior>
                </ibehavior:Interaction.Behaviors>
            </Ellipse>
        </Canvas>
    </Grid>
</Window>
这样就实现了,按住鼠标左键拖动界面上图标的效果了。
代码下载: http://download.csdn.net/detail/yysyangyangyangshan/5422719
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值