Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It is acceptable for your <code>ViewModel</code> to maintain a reference to your <code>Model</code> as the <code>ViewModel</code> can be thought of as a <code>wrapper</code> for your <code>Model</code>.</p> <p>You could put a call to your <code>Model</code> method <code>ReturnMdbTblList</code> such as:</p> <pre><code>public string MdbDir { get { return this.mdbDir; } set { this.mdbDir = value; RaisePropertyChanged("MdbDir"); this.MdbTblList = this.model.ReturnMdbTblList(value); } } </code></pre> <p>This is straight forward to implement and effective. My personal preference is not put anything inside the <code>get</code> and <code>set</code> methods of <code>properties</code> that do not directly affect the <code>field</code> it is accessing or notifying others it has changed. That is just my preference though, others may be happy to do so and I am not saying it is wrong.</p> <p>I would use a <code>DelegateCommand</code> on the button to make the call to your <code>ReturnMdbTdlList</code>:</p> <p><strong>Model, ViewMode &amp; DelegateCommand</strong></p> <pre><code>public class MyViewModel : INotifyPropertyChanged { private readonly MyModel model; private string mdbDir; public string MdbDir { get { return this.mdbDir; } set { this.mdbDir = value; RaisePropertyChanged("MdbDir"); } } private List&lt;string&gt; mdbTblList; public List&lt;string&gt; MdbTblList { get { return this.mdbTblList; } set { this.mdbTblList = value; RaisePropertyChanged("MdbTblList"); } } private DelegateCommand updateMdbTblListCommand; public ICommand UpdateMdbTblListCommand { get { return this.updateMdbTblListCommand ?? (this.updateMdbTblListCommand = new DelegateCommand(this.UpdateMdbTblList)); } } public MyViewModel() { // This would idealy be injected via the constructor this.model = new MyModel(); } private void UpdateMdbTblList(object obj) { var param = obj as string; this.MdbTblList = this.model.ReturnMdbTblList(param); } #region [ INotifyPropertyChanged ] public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChangedInvocator] protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null) { var handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } #endregion } public class MyModel { public List&lt;string&gt; ReturnMdbTblList(string mdbDir) { // Do soemthing return new List&lt;string&gt;(); } } public class DelegateCommand : ICommand { private readonly Predicate&lt;object&gt; _canExecute; private readonly Action&lt;object&gt; _execute; public event EventHandler CanExecuteChanged; public DelegateCommand(Action&lt;object&gt; execute) : this(execute, null) { } public DelegateCommand(Action&lt;object&gt; execute, Predicate&lt;object&gt; canExecute) { _execute = execute; _canExecute = canExecute; } public bool CanExecute(object parameter) { return this._canExecute == null || this._canExecute(parameter); } public void Execute(object parameter) { _execute(parameter); } public void RaiseCanExecuteChanged() { if (CanExecuteChanged != null) { CanExecuteChanged(this, EventArgs.Empty); } } } </code></pre> <p><strong>XAML</strong></p> <pre><code>&lt;StackPanel Orientation="Horizontal" VerticalAlignment="Top"&gt; &lt;TextBox Height="23" Margin="10" Width="200" Text="{Binding MdbDir}" /&gt; &lt;Button Content="Click Me" Width="100" Height="25" Margin="10" Command="{Binding Path=UpdateMdbTblListCommand}" CommandParameter="{Binding Path=MdbDir}" /&gt; &lt;/StackPanel&gt; </code></pre> <p>We bind the <code>Command</code> property of the <code>Button</code> to our <code>UpdateMdbTblCommand</code> in the <code>MyViewModel</code>, we also bind the <code>CommandParameter</code> property of the <code>Button</code> to the <code>MdbDir</code> property of <code>MyViewModel</code>. When the <code>Button</code> is pressed the <code>UpdateMdbTblCommand</code> is executed which in turn calls the <code>UpdateMdbTbl</code> passing along the value of <code>MdbDir</code> as an argument and subsequently updating the <code>MdbTblList</code> property of <code>MyViewModel</code>.</p> <p>As I said the <code>DelegateCommand</code> would be my preferred method, however, it may be overkill when taking into consideration what you have to write to achieve what can be done in the former example.</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