From what I understand, the purpose of the Command pattern is to help separate the interaction of the user interface with the application logic. If the commands are executed correctly, clicking on the "Print" menu item can lead to an interaction chain as follows:
(button) ---click executes command----> (command) ---calls Print() in app logic ---> (logic)
This encourages you to separate the user interface from the application logic.
I watched WPF commands, and for the most part I can see how they implemented this template. However, I feel that to some extent they complicated the Command pattern and were able to implement it in such a way that you are not recommended to separate the interface from the application logic.
For example, consider this simple WPF window, which has a button for inserting text into a text field:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Paste"
Executed="CommandBinding_Executed"/>
</Window.CommandBindings>
<StackPanel>
<TextBox x:Name="txtData" />
<Button Command="Paste" Content="Paste" />
</StackPanel>
</Window>
Here is the code:
namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
ApplicationCommands.Paste.Execute(null, txtData);
}
}
}
? , Click. , Paste, , , ? . , , :
(button) ---executes Routed Command---> (Window) ---executes command binding----(command binding)
(logic) <---calls application logic--- (event handler) <-----raises event --------------|
? .