我如何实现MVVM Light Toolkit EventTrigger [英] How do I implement MVVM Light Toolkit EventTrigger
问题描述
我正在尝试在WPF项目中实现一个Button,它通过单击并通过释放按钮将变量设置为1。因为我发现无法直接绑定到MouseDown和MouseUp。我试图使用MVVM Light Toolkit,但它无法正常工作。有谁知道处理这个? VS2013 WPF4.5
来自MvvmLightLibs.4.2.30.0的我的DLL:
I am trying to implement a Button in a WPF project, which sets a variable to 1 by clicking and back by releasing the button. Since I found no way to bind to MouseDown and MouseUp directly. I am trying to go with MVVM Light Toolkit, but it is not working. Does anyone know to handle this? VS2013 WPF4.5
My DLLs from MvvmLightLibs.4.2.30.0:
GalaSoft.MvvmLight.Extras.WPF45
GalaSoft.MvvmLight.WPF45
System.Windows.Interactivity
我的XAML:
My XAML:
<UserControl x:Class="PMWA.View.OperatorView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF45">
<Grid>
<Button x:Name="MoveUp"
Content="Up"
Margin="5">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<cmd:EventToCommand Command="{Binding MoveUpCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseUp">
<cmd:EventToCommand Command="{Binding StopMoveCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button x:Name="MoveDown"
Content="Down"
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<cmd:EventToCommand Command="{Binding MoveDownCommand}" />
</i:EventTrigger>
<i:EventTrigger EventName="MouseUp">
<cmd:EventToCommand Command="{Binding StopMoveCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button x:Name="Apply"
Content="Apply"
Command="{Binding ApplyDistanceCommand}"
<TextBox x:Name="Distance"
Text="{Binding CurrentDistance}"
</Grid>
</UserControl>
Hier是我的ViewModel:
Hier is my ViewModel:
private ICommand moveUpCommand;
public ICommand MoveUpCommand
{
get
{
if (moveUpCommand == null)
{
moveUpCommand = new RelayCommand(p => ExecuteMoveUpCommand());
}
return moveUpCommand;
}
}
private void ExecuteMoveUpCommand()
{
Trace.WriteLine("Move Up");
SetPort1(1);
}
绑定在MainWindow.xaml.cs中设置如下:
And the binding is set up in MainWindow.xaml.cs like this:
private void SetupBindings()
{
var operatorModel = new Model.Operator();
operatorView.DataContext = new OperatorViewModel(operatorModel, measurementWeightListViewModel);
}
我有第三个按钮,工作正常,但那些上下按钮不会做任何事情:(
我搞砸了这个帖子,所以我在这里发布了:
MVVM Light Toolkit EventTrigger绑定不起作用 [ ^ ]
推荐答案
这篇关于我如何实现MVVM Light Toolkit EventTrigger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!