Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes, there is a clean way to do this. One aspect of using the <a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx" rel="nofollow noreferrer">Model-View-ViewModel pattern</a> in WPF (not that you have to use this) is <em>commanding</em>. <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx" rel="nofollow noreferrer">WPF Commanding reference</a></p> <p>Here is a simplistic but clean and fairly type-safe framework class for exposing commands from your data source object:</p> <pre><code>using System; using System.Windows.Input; namespace MVVM { /// &lt;summary&gt; /// Defines a command that can be bound to from XAML and redirects to a handler function. /// &lt;/summary&gt; public class ViewModelCommand : ICommand { private Action _handler; public ViewModelCommand(Action handler) { _handler = handler; } #region ICommand Members public bool CanExecute(object parameter) { return true; } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { _handler(); } #endregion } /// &lt;summary&gt; /// Defines a command that can be bound to from XAML and redirects to a handler function. /// &lt;/summary&gt; public class ViewModelCommand&lt;T&gt; : ICommand where T : class { private Action&lt;T&gt; _handler; public ViewModelCommand(Action&lt;T&gt; handler) { _handler = handler; } #region ICommand Members public bool CanExecute(object parameter) { return true; } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { _handler(parameter as T); } #endregion } } </code></pre> <p>Your data source (e.g., FootballPlayer class) then exposes a command property as follows:</p> <pre><code> /// &lt;summary&gt; /// Tell the player to run. This particular command takes a string as a parameter. /// &lt;/summary&gt; public ICommand RunCommand { get { return new ViewModelCommand&lt;string&gt;(run); } } </code></pre> <p>The implementation function, in the same FootballPlayer class, can then look like this:</p> <pre><code> /// &lt;summary&gt; /// Tell the player to run. This particular command takes a string as a parameter. /// &lt;/summary&gt; public void search(string destination) { System.Windows.MessageBox.Show(destination, "Running to destination..."); } </code></pre> <p>Finally, your XAML has the following databinding:</p> <pre><code>&lt;Button Content="{Binding PlayerName}" FontSize="16" CommandParameter="{Binding Text, ElementName=txtDestination}" Command="{Binding RunCommand, Source={StaticResource ViewModelDataSource}}" /&gt; </code></pre> <p>(Since you're using a DataTemplate, the sources of the bindings would need to be adjusted; but that's the gist of it. I've used this with great success in a class project - it allowed a very clean separation between the logic and the UI.)</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