行为并不是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