Thomas V Thomas V - 26 days ago 8
C# Question

What event(s) trigger a command in a button?

so i have a question relating to a project i'm working on:

what events are used to trigger a command in a button control? is it only the click event?

because when i click on a button my command will be executed after my mouse button is released? so thats why i want to know.

<Button x:Name="ClickButton" Content="Click here" HorizontalAlignment="Left" Margin="325,123,0,0" VerticalAlignment="Top" Width="75" Command="{Binding ClickHereCommand}"/>


Code in the ViewModel

public ICommand ClickHereCommand => (new CommandHandler(() => IsGoingUp(), _canExecute));

public void IsGoingUp()
{
Console.WriteLine("Moving up...");
}

Answer

By default, the Command property is indeed bound to the Click event. You can bind your Command property to other events as shown here: http://stackoverflow.com/a/20356042/1166719.

Though if you just want to bind your Command to another mouse event, the ClickMode property is what you're looking for: https://msdn.microsoft.com/en-US/library/system.windows.controls.primitives.buttonbase.clickmode(v=vs.110).aspx

Comments