gpt4 book ai didi

WPF MVVM 将事件参数传递给 View 模型

转载 作者:行者123 更新时间:2023-12-01 00:47:17 32 4
gpt4 key购买 nike

我有网格控件,我需要将 MouseWheel 事件传递给 View 模型。

现在我是这样做的

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

</i:Interaction.Triggers>

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

我可以在没有代码和外部库的情况下做到这一点吗?我正在使用 c#、wpf、visual studio 2010 express。

最佳答案

您可以使用带有自定义鼠标手势的输入绑定(bind),这很容易实现:

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>

关于WPF MVVM 将事件参数传递给 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18098769/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com