Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is what I did to work, though I'm not particularly happy with the solution. If anyone knows a better approach, please do let me know.</p> <p>I moved the logic for the commands handler in a separate, static class:</p> <pre><code> static class CommandsCore { public static bool Save_CanExecute(ArticleControl ac) { double baseprice = 0; double.TryParse(ac.ArticleBasePrice.Text, out baseprice); return !string.IsNullOrEmpty(ac.ArticleID.Text) &amp;&amp; !string.IsNullOrEmpty(ac.ArticleName.Text) &amp;&amp; !string.IsNullOrEmpty(ac.ArticleDescription.Text) &amp;&amp; baseprice &gt; 0; } public static void Save_Executed(ArticleControl ac) { ArticleViewModel avm = (ArticleViewModel)ac.DataContext; if (avm != null &amp;&amp; avm.Save()) { ac.ArticleID.Text = String.Empty; ac.ArticleName.Text = String.Empty; ac.ArticleDescription.Text = String.Empty; ac.ArticleBasePrice.Text = String.Empty; } } } </code></pre> <p>I kept the command binding in the user control as it was</p> <pre><code> &lt;UserControl.CommandBindings&gt; &lt;CommandBinding x:Name="saveCmd" Command="local:Commands.Save" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed"/&gt; &lt;/UserControl.CommandBindings&gt; </code></pre> <p>But in the handlers I called the two methods I just defined above.</p> <pre><code> public void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = CommandsCore.Save_CanExecute(this); } public void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) { CommandsCore.Save_Executed(this); } </code></pre> <p>And then I did the same from the window where the control is used.</p> <pre><code>&lt;Window x:Class="MVVMModel.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MVVMModel" Title="MainWindow" Height="350" Width="525"&gt; &lt;Window.CommandBindings&gt; &lt;CommandBinding x:Name="saveCmd" Command="local:Commands.Save" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed"/&gt; &lt;/Window.CommandBindings&gt; &lt;StackPanel&gt; &lt;local:ArticleControl x:Name="articleControl" /&gt; &lt;Button Name="btnSave" Content="Save" Width="100" HorizontalAlignment="Left" Command="local:Commands.Save"/&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>and the handlers</p> <pre><code> public void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = CommandsCore.Save_CanExecute(articleControl); } public void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) { CommandsCore.Save_Executed(articleControl); } </code></pre> <p>And this works, the Save button is enabled only when the fields are filled in appropriately and the command is executed correctly when clicking the button.</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