请问silverlight使用mvvm light如何绑定一个Behavior中的事件

juezhao007 2013-07-30 04:25:43
下面是我自己写的behavior 有两个事件LeftFlick和RightFlick

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;

namespace CardsChina.Behavior
{
public class CustomLRFlickAction : Behavior<UIElement>
{
protected bool MouseDown { get; set; }
Point? downPoint;
//左滑事件
public event EventHandler LeftFlick;
//右滑事件
public event EventHandler RightFlick;
public double MinOffsetProperty
{
get { return (double)GetValue(MinOffsetPropertyProperty); }
set { SetValue(MinOffsetPropertyProperty, value); }
}

// Using a DependencyProperty as the backing store for MaxOffsetProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MinOffsetPropertyProperty =
DependencyProperty.Register("MinOffsetProperty", typeof(double), typeof(CustomLRFlickAction), new PropertyMetadata(20.0));

protected override void OnAttached()
{
base.OnAttached();

this.AssociatedObject.MouseLeftButtonDown += AO_MouseLeftButtonDown;
this.AssociatedObject.MouseLeftButtonUp += AO_MouseLeftButtonUp;
}

protected override void OnDetaching()
{
this.AssociatedObject.MouseLeftButtonDown -= AO_MouseLeftButtonDown;
this.AssociatedObject.MouseLeftButtonUp -= AO_MouseLeftButtonUp;

base.OnDetaching();
}
protected virtual void AO_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (!MouseDown)
{
MouseDown = false;
return;
}
var upPoint = e.GetPosition(null);
if (!downPoint.HasValue)
{
MouseDown = false;
return;
}
var offset = new Point(upPoint.X - downPoint.Value.X, upPoint.Y - downPoint.Value.Y);
System.Diagnostics.Debug.WriteLine("之前点---->{0}", downPoint);
System.Diagnostics.Debug.WriteLine("之后点---->{0}", upPoint);
System.Diagnostics.Debug.WriteLine(offset);
if (offset.Y > MinOffsetProperty)
{
OnRightFlick();
MouseDown = false;
return;
}
if (offset.Y <-MinOffsetProperty)
{
OnLeftFlick();
MouseDown = false;
return;
}
MouseDown = false;
}

protected virtual void AO_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MouseDown = true;
downPoint = e.GetPosition(null);
System.Diagnostics.Debug.WriteLine("之前点---->{0}", downPoint);
}

protected virtual void OnLeftFlick()
{
if (LeftFlick != null)
{
LeftFlick(this.AssociatedObject, EventArgs.Empty);
}
}
protected virtual void OnRightFlick()
{
if (RightFlick != null)
{
RightFlick(this.AssociatedObject, EventArgs.Empty);
}
}
}
}
请问大家 现在我要在Xaml中和viewModel中如何才能实现如触发LeftFlick事件绑定相应方法呢?

我现在在xaml中是如下写的 但是没用
<Rectangle Fill="Blue" Width="400" Height="200" Margin="28,397,28,10">
<i:Interaction.Triggers>
<i:EventTrigger EventName="LeftFlick">
<i:InvokeCommandAction Command="{Binding LeftAnimation}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Rectangle>
...全文
62 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
juezhao007 2013-07-30
  • 打赏
  • 举报
回复
自己顶一下吧。。都沉了

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧