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>
就可以對賦予行為的元素進行拖拽操作:
