Hi matlab22,
You've written your code correctly, but you are trying to overdo it. Just change the button code to this one and let me know how it goes.
<Button x:name="MoveUp" content="Up" margin="5" command="{Binding MoveDownCommand}" xmlns:x="#unknown" />
Hope this helps, Cheers
Update 1
So I understood your question incorrectly (partly due to that you haven't posted all the code I needed to understand it at a glance). Sorry for that.
Your approach to the problem is correct, but your understanding of events are not. That's why you haven't been able to make it work. Events occur when something changes, not as long as something is happening. For instance, when you hold down your left mouse button on the button, it fires the 'LeftMouseButtonDown' event but it won't fire it repeatedly, as long as you are holding it. It would fire it repeatedly, if you started clicking your left mouse button. So what you need is something which would trigger on left mouse button down and keep polling it the status of the left mouse button independently of the UI thread (obvious reason: it will block the UI as long as it runs otherwise).
My explanation has got quite long :D Sorry about that, so here goes the code. I created a new MVVMLight4.5 project and removed all the unnecessary code and updated the MainViewModel and MainWindow to achieve this.
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
statusData = new StringBuilder();
worker = new BackgroundWorker {WorkerSupportsCancellation = true};
worker.DoWork += workerOnDoWork;
MoveUpCommand = new RelayCommand(moveUpAction);
StopMoveUpCommand = new RelayCommand(stopMoveUpAction);
}
private void workerOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
{
var counter = 0;
while (!worker.CancellationPending)
{
if (counter == 100)
break;
Status = "Going up";
Thread.Sleep(50);
counter++;
}
Status = "Stopped going up";
}
private void stopMoveUpAction()
{
worker.CancelAsync();
}
private void moveUpAction()
{
worker.RunWorkerAsync();
}
#region Member(s)
private readonly BackgroundWorker worker;
private readonly StringBuilder statusData;
public string Status
{
get { return statusData.ToString(); }
set
{
if (string.IsNullOrWhiteSpace(value))
return;
statusData.AppendLine(value);
RaisePropertyChanged("Status");
}
}
public RelayCommand MoveUpCommand { get; private set; }
public RelayCommand StopMoveUpCommand { get; private set; }
#endregion
}
And in the MainWindow I changed the XAML to this:
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Content="Go up">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown">
<command:EventToCommand Command="{Binding MoveUpCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="PreviewMouseLeftButtonUp">
<command:EventToCommand Command="{Binding StopMoveUpCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<TextBox Text="{Binding Status}" VerticalScrollBarVisibility="Visible" Grid.Row="1" />
</Grid>
Hope this helps, cheers