Behavior就是可以其它控件拖动
1、添加引用 Interactions 库的。分别为”Microsoft.Expression.Interactions.dll“和”System.Windows.Interactivity.dll“文件。
引用成功:
2、在前端Login.xaml文件中引入命名空间:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
3、新建“CustomBehavior.cs”类:
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media;
namespace WpfBehaviorDeom
{
public class CustomBehavior: Behavior<UIElement>
{
//表示 空件扭曲的效果
SkewTransform _customtransfor;
//通过重载函数进行Behavior附加上时的初始化操作
protected override void OnAttached()
{
base.OnAttached();
_customtransfor = new SkewTransform();
//通过 AssociatedObject属性获取附加的对象
AssociatedObject.RenderTransform = _customtransfor;
AssociatedObject.RenderTransformOrigin = new Point(0.5, 0.5);//扭曲方向
_customtransfor.AngleY = 50;//扭曲角度
}
//通过重载函数进行移除Behavior时的析构操作
protected override void OnDetaching()
{
_customtransfor.AngleY = 0;
base.OnDetaching();
}
}
}
4、前端代码Login.xaml代码如下:
<Image Source="/Img_1.jpg" Width="200" Height="200">
<i:Interaction.Behaviors>
<ei:MouseDragElementBehavior></ei:MouseDragElementBehavior>
<local:CustomBehavior></local:CustomBehavior>
</i:Interaction.Behaviors>
</Image>
执行结果如下: