2013-08-07 46 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>