1、行为
之前讲过的样式是用来设置元素统一属性的,而在更多的用户界面设计中,我们选要用到大量的动画和其他效果,这些代码十分繁杂,但是有很多通用的地方。
为了减轻用户设计的任务,我们使用行为这一特性,将通用的行为内容封装到行为特性中,可以是基本或者复杂的功能,并且可以将该行为应用到其他任意程序。
2、创建行为
我们在一个新的WPF程序中创建行为,首先我们要添加System.Windows.Interactivity.dll的引用,路径为C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.0\Libraries。
然后创建一个新类,定义行为,此处我们以拖拽行为为例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
namespace _29.BehaviorLibrary
{
public class Behavior1:Behavior<UIElement>
{
private Canvas canvas;
//访问放置行为的元素
protected override void OnAttached()
{
base.OnAttached();
//关联事件处理程序
this.AssociatedObject.MouseLeftButtonDown += AssociatedObject_MouseLeftButtonDown;
this.AssociatedObject.MouseMove += AssociatedObject_MouseMove;
this.AssociatedObject.MouseLeftButtonUp += AssociatedObject_MouseLeftButtonUp;
}
protected override void OnDetaching()
{
base.OnDetaching();
//分离事件处理程序
this.AssociatedObject.MouseLeftButtonDown -= AssociatedObject_MouseLeftButtonDown;
this.AssociatedObject.MouseMove -= AssociatedObject_MouseMove;
this.AssociatedObject.MouseLeftButtonUp -= AssociatedObject_MouseLeftButtonUp;
}
private bool isDragging = false;//拖拽模式
private Point mouseOffset;//鼠标位置信息
private void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//找到Canvas面板
if (canvas == null) canvas = VisualTreeHelper.GetParent(this.AssociatedObject) as Canvas;
//设置拖拽模式
isDragging = true;
//获取与点击对象左上角的偏移量
mouseOffset = e.GetPosition(AssociatedObject);
//捕获鼠标
AssociatedObject.CaptureMouse();
}
private void AssociatedObject_MouseMove(object sender, 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);
}
}
private void AssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (isDragging)
{
//释放鼠标并结束拖拽
AssociatedObject.ReleaseMouseCapture();
isDragging = false;
}
}
}
}
3、使用行为
创建好行为后我们新建一个WPF程序,将刚刚的项目添加进来,并且添加引用,同时也要添加System.Windows.Interactivity.dll的引用。
设置命名空间与行为属性:
<Window x:Class="_29.Behavior.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:b="clr-namespace:_29.BehaviorLibrary;assembly=29.BehaviorLibrary"
Title="MainWindow" Height="350" Width="525">
<Canvas Margin="5">
<Rectangle Canvas.Left="10" Canvas.Top="20" Height="80" Name="rectangle1" Fill="Red" Width="80">
<i:Interaction.Behaviors>
<b:Behavior1></b:Behavior1>
</i:Interaction.Behaviors>
</Rectangle>
<Ellipse Canvas.Left="150" Canvas.Top="20" Height="80" Name="ellipse1" Fill="Green" Width="120" />
</Canvas>
</Window>
就可以对赋予行为的元素进行拖拽操作:
