Click here to Skip to main content
15,885,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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:
C#
GalaSoft.MvvmLight.Extras.WPF45
GalaSoft.MvvmLight.WPF45
System.Windows.Interactivity


My XAML:
HTML
<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 is my ViewModel:
C#
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);
}

And the binding is set up in MainWindow.xaml.cs like this:
C#
private void SetupBindings()
{
    var operatorModel = new Model.Operator();
    operatorView.DataContext = new OperatorViewModel(operatorModel, measurementWeightListViewModel);
}

I have i third button which works fine but those up and down buttons wont do anything :(

I messed up in this thread, so i posted it here:

MVVM Light Toolkit EventTrigger binding doesnt work[^]
Posted
Updated 19-Mar-14 5:28am
v4
Comments
Sergey Alexandrovich Kryukov 12-Mar-14 12:10pm    
Are those the errors on the XAML file? You need to add appropriate XML namespaces...
—SA
matlab22 13-Mar-14 3:15am    
Yes those are the xaml errors. I figured it out. The references dlls were missing:
GalaSoft.MvvmLight.Extras.WPF45
GalaSoft.MvvmLight.WPF45
System.Windows.Interactivity

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900