Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Why dont you use the Command Property of the Button instead of the events? </p> <p>The Command is triggerd by mouse click or enter key.</p> <p><a href="http://msdn.microsoft.com/en-us/library/ms752308.aspx" rel="nofollow">Commanding Overview</a></p> <p><a href="http://msdn.microsoft.com/en-us/library/system.windows.input.commandbinding.aspx" rel="nofollow">CommandBinding</a></p> <p>Xaml:</p> <pre><code> &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Button Grid.Row="0" Command="{Binding CalculationCommand}" Content=" Calculate" /&gt; &lt;Label Grid.Row="1" Content="{Binding StateText}" /&gt; &lt;/Grid&gt; </code></pre> <p>ViewModel:</p> <pre><code>public class ViewModel : INotifyPropertyChanged { private string _stateText; public RelayCommand CalculationCommand { get; set; } public string StateText { get { return _stateText; } set { _stateText = value; OnPropertyChanged("StateText");} } public ViewModel() { CalculationCommand = new RelayCommand(OnCalculate); StateText = string.Empty; } private void OnCalculate(object obj) { StateText = "Please wait, calculating."; var context = TaskScheduler.Current; Task.Factory.StartNew(() =&gt;{ //Calculating Logic goes here }).ContinueWith(x =&gt; { StateText = "Done."; },context); } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } } </code></pre> <p>I am using the RelayCommand from Josh Smith <a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx" rel="nofollow">article</a> </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload