我正在进行MVVM项目,遇到了这样的问题:在某些附加事件的路由之后,我丢失了EventArgs。对于RoutingEvent,我使用了以下代码:
public class RoutedEventTrigger : EventTriggerBase<DependencyObject>
{
RoutedEvent _routedEvent;
public RoutedEvent RoutedEvent
{
get { return _routedEvent; }
set { _routedEvent = value; }
}
public RoutedEventTrigger()
{
}
protected override void OnAttached()
{
Behavior behavior = base.AssociatedObject as Behavior;
FrameworkElement associatedElement = base.AssociatedObject as FrameworkElement;
if (behavior != null)
{
associatedElement = ((IAttachedObject)behavior).AssociatedObject as FrameworkElement;
}
if (associatedElement == null)
{
throw new ArgumentException("Routed Event trigger can only be associated to framework elements");
}
if (RoutedEvent != null)
{
associatedElement.AddHandler(RoutedEvent, new RoutedEventHandler(this.OnRoutedEvent));
}
}
void OnRoutedEvent(object sender, RoutedEventArgs args)
{
base.OnEvent(args);
}
protected override string GetEventName()
{
return RoutedEvent.Name;
}
}
XAML的一部分:
<ListView>
<i:Interaction.Triggers>
<localP:RoutedEventTrigger RoutedEvent="ScrollViewer.ScrollChanged">
<cmd:EventToCommand Command="{Binding ScrollCommand}" />
</localP:RoutedEventTrigger>
</i:Interaction.Triggers>
MVVM中我的处理程序:
ScrollCommand = new RelayCommand<ScrollChangedEventArgs>(e =>
{
MessageBox.Show("HorizontalChange " + e.HorizontalChange");
});
Handler被调用,但e为空。我做错了什么,如何获取那些Args?
作为MVVM基础,我使用mvvm-light。