Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF: using Commands bound in a UserControl
    primarykey
    data
    text
    <p>I'm doing a sample with MVVM and have a problem with commands. I have an Article class (with ID, Name, Price, etc.), an ArticleViewModel that represents the view model, and a user control (ArticleControl) that allows to input the data for the article, with bindings to the properties of the ArticleViewModel. This user control has a biding for a save command.</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>This is how the command is defined:</p> <pre><code> public class Commands { private static RoutedUICommand _save; public static RoutedUICommand Save { get { return _save; } } static Commands() { InputGestureCollection saveInputs = new InputGestureCollection(); saveInputs.Add(new KeyGesture(Key.S, ModifierKeys.Control, "Ctrl+S")); _save = new RoutedUICommand( "Save", "Save", typeof(Commands), saveInputs); } } </code></pre> <p>And the command binding handlers:</p> <pre><code> private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e) { double baseprice = 0; double.TryParse(ArticleBasePrice.Text, out baseprice); e.CanExecute = !string.IsNullOrEmpty(ArticleID.Text) &amp;&amp; !string.IsNullOrEmpty(ArticleName.Text) &amp;&amp; !string.IsNullOrEmpty(ArticleDescription.Text) &amp;&amp; baseprice &gt; 0; } private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) { ArticleViewModel avm = (ArticleViewModel)DataContext; if (avm != null &amp;&amp; avm.Save()) { ArticleID.Text = String.Empty; ArticleName.Text = String.Empty; ArticleDescription.Text = String.Empty; ArticleBasePrice.Text = String.Empty; } } </code></pre> <p>Now, I put this user control on a window. When I hit Ctrl+S the command is executed. However, I also put a Save button on that window, next to this user control. When I click it I want to execute the same command (and I don't want to do another command binding in the window where the user control is hosted).</p> <pre><code> &lt;StackPanel&gt; &lt;local:ArticleControl x:Name="articleControl" /&gt; &lt;Button Name="btnSave" Content="Save" Width="100" HorizontalAlignment="Left" Command="{???}"/&gt; &lt;!-- what should I put here? --&gt; &lt;/StackPanel&gt; </code></pre> <p>But I do not know how to refer that saveCmd defined in the user control. I tried different things, some are completely wrong (they throw exception when running the app), some don't have any effect.</p> <pre><code>Command="{StaticResource saveCmd}" Command="{StaticResource local:ArticleControl.saveCmd}" Command="{x:Static local:Commands.Save}" </code></pre> <p>Any help is appreciated. Thank you.</p>
    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.
    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