我正在做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");
});
调用处理程序,但e为null。 我做错了什么,我怎么能得到那个Args?
作为mvvm base我使用的是mvvm-light。
答案 0 :(得分:4)
您好,您应该在EventToCommand上将PassEventArgsToCommand设置为true。