Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The answer to your question is very simple: Use <code>Commands</code>.</p> <p>Commands are MVVM's way to realize the binding to a method in your ViewModel. The implementation of Commands follows a very standard pattern. You will find plenty of information over the Internet here is just a short sketch:</p> <p>Commands implemented in your ViewModel have to be of type <code>ICommand</code> and every Command has to come along with to methods in your code one responsible for executing the actual method and the other one for checking if the execution is currently possible.</p> <p>These methods have to be named <code>CanExecute</code> and <code>Execute</code> respectively. It is commonly the case to facilitate the use of several Commands with a small helping class called <code>DelegateCommand</code> which provides delegates for the previously mentioned methods.</p> <p>Take this class as it is without any modifications:</p> <pre><code>public class DelegateCommand&lt;T&gt; : ICommand { private Predicate&lt;T&gt; canExecute; private Action&lt;T&gt; execute; public event EventHandler CanExecuteChanged; public DelegateCommand (Predicate&lt;T&gt; canExecute, Action&lt;T&gt; execute) { this.canExecute = canExecute; this.execute = execute; } public bool CanExecute (object param) { return canExecute((T)param); } public void Execute (object param) { execute((T)param); } public void CanExecuteChangedRaised () { CanExecuteChanged(this, new EventArgs()); } } </code></pre> <p>Then your Command declarations are of type <code>DelegateCommand</code> rather than of type <code>ICommand</code>. See the following example to illustrate and you will get the idea:</p> <p>Supose you have a method foo() in your ViewModel you want to be called with a click to a button:</p> <pre><code>class ViewModel { // ... public DelegateCommand&lt;object&gt; FooCommand { get; set; } public ViewModel () { FooCommand = new DelegateCommand&lt;object&gt;(CanExecuteFooCommand, ExecuteFooCommand); } public bool CanExecuteFooCommand (object param) { return true; } public void ExecuteFooCommand (object param) { foo(); } // ... } </code></pre> <p>Supposing you have set your ViewModel as the controls <code>DataContext</code> via it's <code>DataContext</code> property the only thing left to do is to bind the <code>FooCommand</code> to your button like this:</p> <p></p> <p>That's it!</p> <p><strong>APPENDIX (referring to comment):</strong></p> <p>In order to have some action take place without actually hitting the Button you would simply have to track any changed in the UI with your ViewModel and react accordingly - that's what MVVM is about: Track the data from the UI modify or process them and populate them back to the UI.</p> <p>To react on a TextBox Text change create a corresponding string property in your ViewModel and track whether the new ioncoming value from the View is different to the current textBox text:</p> <pre><code>private string _text; public string Text { get { return _text; } set { // the text in the TextBox is about to change. if (!_text.Equals(value)) { doSomething(); } _text = value; FirePropertyChanged("Text"); } } </code></pre> <p>For doing the same with your CheckBox you can apply ICommand as described above since CheckBox is derived from Button and is therefor offering the Command property.</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