Use TextBox.CommandBindingst to bind command


   
  
<Window x:Class="Commands.TwoDocument"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TwoDocument" Height="300" Width="300">

  <Window.Resources>
    <CommandBinding x:Key="binding" Command="ApplicationCommands.Save" Executed="SaveCommand" CanExecute="SaveCommand_CanExecute" />
  </Window.Resources>

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Menu Grid.Row="0">
      <MenuItem Header="File">
        <MenuItem Command="New"></MenuItem>
        <MenuItem Command="Open"></MenuItem>
        <MenuItem Command="Save"></MenuItem>
        <MenuItem Command="SaveAs"></MenuItem>
        <Separator></Separator>
        <MenuItem Command="Close"></MenuItem>
      </MenuItem>
    </Menu>

    <TextBox Margin="5" Grid.Row="3" TextWrapping="Wrap" AcceptsReturn="True" TextChanged="txt_TextChanged">
      <TextBox.CommandBindings>
        <StaticResource ResourceKey="binding"></StaticResource>
        <CommandBinding Command="ApplicationCommands.Save" Executed="SaveCommand" />
      </TextBox.CommandBindings>
    </TextBox>

  </Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Commands
{
    public partial class TwoDocument : System.Windows.Window
    {
        public TwoDocument()
        {
            InitializeComponent();
        }

        private void SaveCommand(object sender, ExecutedRoutedEventArgs e)
        {            
        }
        private void txt_TextChanged(object sender, RoutedEventArgs e)
        {
        }
        private void SaveCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
        }
    }
}

   
    
     


Bind TextBox save command to CommandBinding


   
  
<Window x:Class="Commands.TwoDocument"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TwoDocument" Height="300" Width="300">

  <Window.Resources>
    <CommandBinding x:Key="binding" Command="ApplicationCommands.Save" Executed="SaveCommand" CanExecute="SaveCommand_CanExecute" />
  </Window.Resources>

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Menu Grid.Row="0">
      <MenuItem Header="File">
        <MenuItem Command="New"></MenuItem>
        <MenuItem Command="Open"></MenuItem>
        <MenuItem Command="Save"></MenuItem>
        <MenuItem Command="SaveAs"></MenuItem>
        <Separator></Separator>
        <MenuItem Command="Close"></MenuItem>
      </MenuItem>
    </Menu>

    <TextBox Margin="5" Grid.Row="2" TextWrapping="Wrap" AcceptsReturn="True" TextChanged="txt_TextChanged">
      <TextBox.CommandBindings>
        <StaticResource ResourceKey="binding"></StaticResource>
      </TextBox.CommandBindings>
    </TextBox>

  </Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Commands
{
    public partial class TwoDocument : System.Windows.Window
    {
        public TwoDocument()
        {
            InitializeComponent();
        }

        private void SaveCommand(object sender, ExecutedRoutedEventArgs e)
        {            
        }
        private void txt_TextChanged(object sender, RoutedEventArgs e)
        {
        }
        private void SaveCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
        }
    }
}

   
    
     


Create CommandBindings in Xaml and bind to Button


   
  
<Window x:Class="Commands.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Commands" Height="300" Width="300">
    <Grid>
      <StackPanel>
        <StackPanel.CommandBindings>
          <CommandBinding Command="ApplicationCommands.Paste" Executed="InvokeApplicationCommand"/>
        </StackPanel.CommandBindings>
        <Button Command="ApplicationCommands.Paste">
          Paste Something (Control + V) when I have the focus!"
        </Button>
        
      </StackPanel>
      
    </Grid>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace Commands
{
    public partial class Window1 : System.Windows.Window
    {

        public Window1()
        {
            InitializeComponent();
        }

        void InvokeApplicationCommand(object target, ExecutedRoutedEventArgs args)
        {
            MessageBox.Show("The ApplicationCommand has been invoked.");
        }
    }
}

   
    
     


Using CommandBinding and ExecutedRoutedEventHandler to bind Application event to an event handler method


   
  


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="CommandBasics" Height="300" Width="300">
  <Grid>
    <Button Command="ApplicationCommands.Properties" Content="_Properties"/>
  </Grid>

</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {

        public Window1()
        {
            InitializeComponent();

            CommandBinding cb = new CommandBinding(ApplicationCommands.Properties);
            cb.Executed += new ExecutedRoutedEventHandler(cb_Executed);
            this.CommandBindings.Add(cb);

        }

        void cb_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Properties");
        }
    }
}

   
    
     


Command Enabling


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="CommandEnabling" Height="300" Width="300"
    >
  <Grid>
    <Menu VerticalAlignment="Top">
      <MenuItem Header="_Edit">
        <MenuItem Command="Redo" />
      </MenuItem>
    </Menu>
  </Grid>
</Window>



//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace WpfApplication1
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();

            CommandBinding redoCommandBinding = new CommandBinding(ApplicationCommands.Redo);
            redoCommandBinding.CanExecute += RedoCommandCanExecute;
            CommandBindings.Add(redoCommandBinding);
        }

        void RedoCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute =  DateTime.Now.Second % 2 > 1;
        }
    }
}

   
    
     


Bind the Button to a Command


   
  
<Window x:Class="WpfApplication1.Window1"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Title="WPF" Height="240" Width="300">
    <StackPanel>
        <TextBlock Text="First Name"/>
        <TextBox   Text="{Binding Path=FirstName}"/>
        <TextBlock Text="Age"/>
        <TextBox Text="{Binding Path=Age}"/>
        
        <Button Command="{Binding Path=Add}" Content="Add"/>
        <ComboBox x:Name="cboOccupation" IsEditable="False" Width="100">
             <ComboBoxItem>Student</ComboBoxItem>
             <ComboBoxItem>Skilled</ComboBoxItem>
             <ComboBoxItem>Professional</ComboBoxItem>
        </ComboBox>
        <Button Command="{Binding Path=SetOccupation}" CommandParameter="{Binding ElementName=cboOccupation, Path=Text}" Content="Set Occupation"/>
        <TextBlock Text="Status"/>
        <TextBlock Text="{Binding Path=Status, UpdateSourceTrigger=PropertyChanged}"/>
        
    </StackPanel>
</Window>

//File:Window.xaml.cs

using System.Windows;
using System;
using System.ComponentModel;
using System.Windows.Input;
namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            this.DataContext = new Employee(){FirstName = "A"};
        }
    }
    public class Employee : INotifyPropertyChanged
    {
        private string firstName;
        private int age;
        private string occupation;
        private string status;

        private AddEmployeeCommand addEmployeeCommand;
        private SetOccupationCommand setOccupationCommand;
        public string Status
        {
            get
            {
                return status;
            }
            set
            {
                if(status!= value)
                {
                    status= value;
                    OnPropertyChanged("Status");
                }
            }
        }
        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                if(firstName != value)
                {
                    firstName = value;
                    OnPropertyChanged("FirstName");
                }
            }
        }
        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                if(this.age != value)
                {
                    this.age = value;
                    OnPropertyChanged("Age");
                }
            }
        }
        public string Occupation
        {
            get
            {
                return occupation;
            }
            set
            {
                if(this.occupation != value)
                {
                    this.occupation = value;
                    OnPropertyChanged("Occupation");
                }
            }
        }
        public AddEmployeeCommand Add
        {
            get
            {
                if(addEmployeeCommand == null)
                    addEmployeeCommand = new AddEmployeeCommand(this);

                return addEmployeeCommand;
            }
        }
        public SetOccupationCommand SetOccupation
        {
            get
            {
                if(setOccupationCommand == null)
                    setOccupationCommand = new SetOccupationCommand(this);

                return setOccupationCommand;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if(this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    public class AddEmployeeCommand : ICommand
    {
        private Employee person;

        public AddEmployeeCommand(Employee person)
        {
            this.person = person;

            this.person.PropertyChanged += new PropertyChangedEventHandler(person_PropertyChanged);
        }

        private void person_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if(CanExecuteChanged != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }
        public bool CanExecute(object parameter)
        {
            if(!string.IsNullOrEmpty(person.FirstName))
               if(person.Age > 0)
                    if(string.IsNullOrEmpty(person.Status))
                            return true;

            return false;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            person.Status = string.Format("Added {0}", person.FirstName);
        }
    }

    public class SetOccupationCommand : ICommand
    {
        private Employee person;

        public SetOccupationCommand(Employee person)
        {
            this.person = person;

            this.person.PropertyChanged += new PropertyChangedEventHandler(person_PropertyChanged);
        }

        private void person_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if(CanExecuteChanged != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }

        public bool CanExecute(object parameter)
        {
            if(!string.IsNullOrEmpty(parameter as string))
                if(!string.IsNullOrEmpty(person.Status))
                    return true;

            return false;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            person.Occupation = parameter.ToString();

            person.Status = string.Format("Added {0},{1}", person.FirstName, person.Occupation);
        }
    }
}

   
    
     


Command Handler Key Binding


   
  



<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="commandWithHandler" Name="root">
  
  <Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Open"
                    Executed="OpenCmdExecuted"
                    CanExecute="OpenCmdCanExecute"/>
  </Window.CommandBindings>


  <Window.InputBindings>
    <KeyBinding Command="ApplicationCommands.Open" Gesture="CTRL+R" />
  </Window.InputBindings>

  <StackPanel>
    <Button Command="ApplicationCommands.Open" Name="MyButton"
            Height="50" Width="200">
      Open (KeyBindings: Ctrl+R, Ctrl+0)
    </Button>
  </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        void OpenCmdExecuted(object target, ExecutedRoutedEventArgs e)
        {
            String command, targetobj;
            command = ((RoutedCommand)e.Command).Name;
            targetobj = ((FrameworkElement)target).Name;
            MessageBox.Show(command +  " : " + targetobj);
        }
        void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
    }
}