我正在做 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 为空。我做错了什么,我怎么能得到那个 Args?
作为 mvvm 基础,我使用 mvvm-light。