WPF: Using UserControl Related Commands

I am doing a sample with MVVM and have problems with commands. I have an Article class (with identifier, name, price, etc.), an ArticleViewModel that represents the presentation model, and a user control (ArticleControl) that allows you to enter data for the article, with associations with the properties of ArticleViewModel, This custom element Management has a bid for the save team.

   <UserControl.CommandBindings>
      <CommandBinding x:Name="saveCmd" 
                      Command="local:Commands.Save" 
                      CanExecute="CommandBinding_CanExecute"
                      Executed="CommandBinding_Executed"/>
   </UserControl.CommandBindings>

This is how the command is defined:

   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);
      }
   }

And command binding handlers:

  private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  {
     double baseprice = 0;
     double.TryParse(ArticleBasePrice.Text, out baseprice);

     e.CanExecute =
        !string.IsNullOrEmpty(ArticleID.Text) &&
        !string.IsNullOrEmpty(ArticleName.Text) &&
        !string.IsNullOrEmpty(ArticleDescription.Text) &&
        baseprice > 0;
  }

  private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
  {
     ArticleViewModel avm = (ArticleViewModel)DataContext;
     if (avm != null && avm.Save())
     {
        ArticleID.Text = String.Empty;
        ArticleName.Text = String.Empty;
        ArticleDescription.Text = String.Empty;
        ArticleBasePrice.Text = String.Empty;
     }
  }

. Ctrl + S, . "" . , ( , ).

   <StackPanel>
      <local:ArticleControl x:Name="articleControl" />
      <Button Name="btnSave" 
              Content="Save" Width="100" 
              HorizontalAlignment="Left" 
              Command="{???}"/> <!-- what should I put here? -->
   </StackPanel>

, saveCmd, . , ( ), .

Command="{StaticResource saveCmd}"
Command="{StaticResource local:ArticleControl.saveCmd}"
Command="{x:Static local:Commands.Save}"

. .

+3
4

, "" , , "" , . () , CommandBindings.

, CommandTarget "" .

- FocusManager.IsFocusScope=True . , , IsFocusScope, , , , . , .

+3

, CommandBinding , UserControl!

+1

, , :

  • , .

  • Command, CommandTarget FocusManager , (ArticleUserControl - x:Name ).

XAML :

<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">
   <StackPanel>
      <local:ArticleControl x:Name="articleControl" />
      <Button Name="btnSave" Content="Save" Width="100" HorizontalAlignment="Left" 
              Command="local:Commands.Save"
              CommandTarget="{Binding ElementName=ArticleUserControl}"
              FocusManager.IsFocusScope="True" />
   </StackPanel>
</Window>
+1

, , . - , , .

:

   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) &&
            !string.IsNullOrEmpty(ac.ArticleName.Text) &&
            !string.IsNullOrEmpty(ac.ArticleDescription.Text) &&
            baseprice > 0;
      }

      public static void Save_Executed(ArticleControl ac)
      {
         ArticleViewModel avm = (ArticleViewModel)ac.DataContext;
         if (avm != null && avm.Save())
         {
            ac.ArticleID.Text = String.Empty;
            ac.ArticleName.Text = String.Empty;
            ac.ArticleDescription.Text = String.Empty;
            ac.ArticleBasePrice.Text = String.Empty;
         }
      }
   }

,

   <UserControl.CommandBindings>
      <CommandBinding x:Name="saveCmd" 
                      Command="local:Commands.Save" 
                      CanExecute="CommandBinding_CanExecute"
                      Executed="CommandBinding_Executed"/>
   </UserControl.CommandBindings>

, .

  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);
  }

, .

<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">
   <Window.CommandBindings>
      <CommandBinding x:Name="saveCmd" 
                      Command="local:Commands.Save" 
                      CanExecute="CommandBinding_CanExecute"
                      Executed="CommandBinding_Executed"/>
   </Window.CommandBindings>

   <StackPanel>
      <local:ArticleControl x:Name="articleControl" />
      <Button Name="btnSave" Content="Save" Width="100" HorizontalAlignment="Left" 
              Command="local:Commands.Save"/>
   </StackPanel>
</Window>

  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);
  }

, "" , , .

0

All Articles