2013-08-07 45 views
2

我有网格控件,我需要传递MouseWheel事件来查看模型。WPF MVVM传递事件参数来查看模型

现在我这样做是这样的

<i:Interaction.Triggers> 
     <i:EventTrigger EventName="MouseWheel"> 
      <i:InvokeCommandAction Command="{Binding MouseWheelCommand}" /> 
     </i:EventTrigger> 

    </i:Interaction.Triggers> 

,但我需要做的鼠标不同的动作向上滚动鼠标向下滚动。 如何做到这一点?

我可以在没有代码的情况下做到这一点,没有外部库吗?我使用C#,WPF,Visual Studio 2010 express。

回答

0

为此,您需要在您的MVVM中使用MouseWheelEventArgs。所以传递这个EventArgs作为commandParamter。 您可以参考以下链接--- Passing EventArgs as CommandParameter

然后在您的视图模型类,你可以使用此事件参数如下

void Scroll_MouseWheel(MouseWheelEventArgs e) 
    { 
     if (e.Delta > 0) 
     { 
      // Mouse Wheel Up Action 
     } 
     else 
     { 
      // Mouse Wheel Down Action 
     } 

     e.Handled = true; 
    } 
0

您可以使用输入绑定使用自定义鼠标手势,这是很容易实现:

public class MouseWheelUp : MouseGesture 
{ 
    public MouseWheelUp(): base(MouseAction.WheelClick) 
    { 
    } 

    public MouseWheelUp(ModifierKeys modifiers) : base(MouseAction.WheelClick, modifiers) 
    { 
    }  

    public override bool Matches(object targetElement, InputEventArgs inputEventArgs) 
    { 
     if (!base.Matches(targetElement, inputEventArgs)) return false; 
     if (!(inputEventArgs is MouseWheelEventArgs)) return false; 
     var args = (MouseWheelEventArgs)inputEventArgs; 
     return args.Delta > 0;  
    } 
} 

,然后用它是这样的:

<TextBlock> 
     <TextBlock.InputBindings> 

      <MouseBinding Command="{Binding Command}"> 
       <MouseBinding.Gesture> 
        <me:MouseWheelUp /> 
       </MouseBinding.Gesture> 
      </MouseBinding> 

     </TextBlock.InputBindings> 
     ABCEFG 
</TextBlock>