Bind ApplicationCommand to a handler

image_pdfimage_print


   
 

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

   
     


Assign ApplicationCommands.Open to Button

image_pdfimage_print


   
 



<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="CommandHandlerProcedural"
    >
</Window>
//File:Window.xaml.cs

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

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            
            StackPanel MainStackPanel = new StackPanel();
            this.AddChild(MainStackPanel);

            Button CommandButton = new Button();
            CommandButton.Command = ApplicationCommands.Open;
            CommandButton.Content = "Open (KeyBindings: Ctrl-R, Ctrl-0)";
            MainStackPanel.Children.Add(CommandButton);

            CommandBinding OpenCmdBinding = new CommandBinding(ApplicationCommands.Open,OpenCmdExecuted,OpenCmdCanExecute);

            this.CommandBindings.Add(OpenCmdBinding);

            KeyBinding OpenCmdKeyBinding = new KeyBinding(ApplicationCommands.Open,Key.R,ModifierKeys.Control);

            this.InputBindings.Add(OpenCmdKeyBinding);
        }

        void OpenCmdExecuted(object target, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("The command has been invoked.");
        }

        void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
    }
}

   
     


Binding command to ApplicationCommands.Redo

image_pdfimage_print


   
 

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

   
     


Binding Command to ApplicationCommands.New

image_pdfimage_print


   
 

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="CommandHandling" Height="300" Width="300"
    >
    <DockPanel>
      <Menu DockPanel.Dock="Top">
        <MenuItem Header="_File">
          <MenuItem Command="New" />
        </MenuItem>
      </Menu>
      <TextBox x:Name="inputBox" TextChanged="OnTextboxTextChanged" />
    </DockPanel>
</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
    {
        bool unsavedChanges = false;
        
        public Window1()
        {
            InitializeComponent();

            CommandBinding cmdBindingNew = new CommandBinding(ApplicationCommands.New);
            cmdBindingNew.Executed += NewCommandHandler;
            CommandBindings.Add(cmdBindingNew);
        }

        void NewCommandHandler(object sender, ExecutedRoutedEventArgs e)
        {
            if (unsavedChanges)
            {
                MessageBoxResult result = MessageBox.Show(this,"Save changes to existing document?", "New",MessageBoxButton.YesNoCancel);

                if (result == MessageBoxResult.Cancel){
                    return;
                }
                if (result == MessageBoxResult.Yes){
                    SaveChanges();
                }
            }
            inputBox.Clear();
        }
        private void OnTextboxTextChanged(object sender, RoutedEventArgs e){
            unsavedChanges = true;
            Console.WriteLine("changed");
        }

        private void SaveChanges(){
            unsavedChanges = false;
            Console.WriteLine("saved");
        }

    }
}

   
     


Bind CanExecute to ApplicationCommands.Save

image_pdfimage_print


   
 

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

   
     


Binding ApplicationCommands.New Command to your own handler

image_pdfimage_print


   
 
<Window x:Class="Commands.TestNewCommand"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TestNewCommand" Height="150" Width="300">
  <Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.New" Executed="NewCommand" />
  </Window.CommandBindings>

  <StackPanel >
    <Menu>
      <MenuItem Header="File">
        <MenuItem Command="New"></MenuItem>
      </MenuItem>
    </Menu>

    <Button Margin="5" Padding="5" Command="ApplicationCommands.New" ToolTip="{x:Static ApplicationCommands.New}">New</Button>
    <Button Margin="5" Padding="5" Visibility="Hidden" Command="ApplicationCommands.Open">Open (unwired)</Button>
    <Button Margin="5" Padding="5" Visibility="Hidden" Click="cmdDoCommand_Click" >DoCommand</Button>
    </StackPanel>
</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 TestNewCommand : System.Windows.Window
    {
        public TestNewCommand()
        {
            InitializeComponent();
        }
        private void NewCommand(object sender, ExecutedRoutedEventArgs e)
        {            
            MessageBox.Show("New command triggered by " + e.Source.ToString());
        }
        private void cmdDoCommand_Click(object sender, RoutedEventArgs e)
        {
            this.CommandBindings[0].Command.Execute(null);
        }
    }
}

   
     


Change ApplicationCommands.New.Text

image_pdfimage_print


   
 
<Window x:Class="Commands.TestNewCommand"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TestNewCommand" Height="150" Width="300">
  <Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.New"
      Executed="NewCommand" />
  </Window.CommandBindings>

  <StackPanel >
    <Menu>
      <MenuItem Header="File">
        <MenuItem Command="New"></MenuItem>
      </MenuItem>
    </Menu>

    <Button Margin="5" Padding="5" Command="ApplicationCommands.New" ToolTip="{x:Static ApplicationCommands.New}">New</Button>
    <Button Margin="5" Padding="5" Visibility="Hidden" Command="ApplicationCommands.Open">Open (unwired)</Button>
      <Button Margin="5" Padding="5" Visibility="Hidden" Click="cmdDoCommand_Click" >DoCommand</Button>
    </StackPanel>
</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 TestNewCommand : System.Windows.Window
    {
        public TestNewCommand()
        {
            ApplicationCommands.New.Text = "NewNew";
            InitializeComponent();
        }
        private void NewCommand(object sender, ExecutedRoutedEventArgs e)
        {            
            MessageBox.Show("New command triggered by " + e.Source.ToString());
        }
        private void cmdDoCommand_Click(object sender, RoutedEventArgs e)
        {
            this.CommandBindings[0].Command.Execute(null);
        }
    }
}