使用已经存在的behaviors
1引入DLL
2在XAML中添加行为,
在这里添加了2个,一个是拖动,一个是缓动效果
Height="20"
Width="20"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="20"
Fill="BlueViolet">
<i:Interaction.Behaviors>
<ei:MouseDragElementBehavior> 拖拽效果行为
</ei:MouseDragElementBehavior>
<ei:FluidMoveBehavior Duration="0:0:4">缓动
<ei:FluidMoveBehavior.EaseX>
<ElasticEase EasingMode="EaseOut"
Oscillations="3"
Springiness="4" />
</ei:FluidMoveBehavior.EaseX>
<ei:FluidMoveBehavior.EaseY>
<ElasticEase EasingMode="EaseOut"
Oscillations="3"
Springiness="4" />
</ei:FluidMoveBehavior.EaseY>
</ei:FluidMoveBehavior>
</i:Interaction.Behaviors>
Thickness margin = PurpleSquare.Margin;
margin.Left += 100;
margin.Top += 100;
PurpleSquare.Margin = margin;
}
自定义behaviors
public class CustomBehavior : Behavior<Button>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Click += new RoutedEventHandler(OnButtonClick);
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.Click -= OnButtonClick;
}
void OnButtonClick(object sender, RoutedEventArgs e)
{
MessageBox.Show("自定义行为-! MessageBox.Show");
}
}
源码下载
https://files.cnblogs.com/facingwaller/Behaviors.rar
扩展阅读
BLEND下的Behaviors