Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your view you can add a "Command" property in xaml and bind it to your ViewModel's command</p> <pre><code>Command="{Binding CustomerSelectedCommand}" </code></pre> <p>Parameters can be passed in multiple ways. Most of the time, I just have other items bound to my ViewModel and I can just use them directly. However there is also a property called CommandParameter, here's an example of specifying it in XAML.</p> <pre><code> CommandParameter="{Binding ElementName=txtPassword}" </code></pre> <p>then in my ViewModel the definition of my Command looks like this</p> <pre><code>private void UserLogonCommandExecute(object parameter) { ... var password_box = parameter as PasswordBox; ... } </code></pre> <p>It sounds like you already know how to set up a RelayCommand in your ViewModel so I won't go into that. I found <a href="http://windowsclient.net/learn/video.aspx?v=314683" rel="nofollow">How Do I: Build Data-driven WPF Application using the MVVM pattern</a> helpful when I was getting started. </p> <h1>Per Comment Request Command Property Example</h1> <p>I'm just going to grab some working code, here's how you add a Command property to a button in XAML. </p> <pre><code>&lt;Button Command="{Binding ConnectCommand}"&gt; //Your button content and closing &lt;/Button&gt; here </code></pre> <p>This assume you have set your DataContext to a ViewModel that has a Command called ConnectCommand. Here's an example for ConnectCommand. You'll need to replace the contents of ConnectCommandCanExecute and ConnectCommandExecute with whatever work you want done. </p> <pre><code>public ICommand ConnectCommand { get { if (_connectCommand == null) { _connectCommand = new RelayCommand(param =&gt; ConnectCommandExecute(), param =&gt; ConnectCommandCanExecute); } return _connectCommand; } } private bool ConnectCommandCanExecute { get { return !_instrumentModel.IsConnected; } } private void ConnectCommandExecute() { if (TcpSettingsChanged()) SaveTcpSettings(); _instrumentModel.Connect(_tcpData); } </code></pre> <h1>RelayClass</h1> <p>One part of making this simple is the RelayClass I have in one of my core library .dlls. I probably got this from one of the videos I watched. This can be cut and pasted in it's entirety, there is nothing here you need to customize, except you'll probably want to change the namespace this is in.</p> <pre><code>using System; using System.Diagnostics; using System.Windows.Input; namespace Syncor.MvvmLib { public class RelayCommand : ICommand { private readonly Action&lt;object&gt; _execute; private readonly Predicate&lt;object&gt; _canExecute; public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public RelayCommand(Action&lt;object&gt; execute) : this(execute, (Predicate&lt;object&gt;) null) { this._execute = execute; } public RelayCommand(Action&lt;object&gt; execute, Predicate&lt;object&gt; canExecute) { if (execute == null) throw new ArgumentNullException("execute"); this._execute = execute; this._canExecute = canExecute; } [DebuggerStepThrough] public bool CanExecute(object parameter) { if (this._canExecute != null) return this._canExecute(parameter); else return true; } public void Execute(object parameter) { this._execute(parameter); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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