Live Chat Icon For mobile
Live Chat Icon

WPF FAQ - Input and Commands.

Find answers for the most frequently asked questions
Expand All Collapse All

CommandBinding can be added to a window using markup. Note that in XAML, the CommandBindingCollection is not declared in the markup as an element and the collection object is inferred by the type that the property takes and you populate the property element with one or more CommandBinding elements.


  <CommandBinding Command='ApplicationCommands.Open'

You can do so by listening to the ComponentDispatcher.ThreadPreprocessMessage in your window as follows:

using System;
using System.Windows;
using System.Windows.Controls;
using swf = System.Windows.Forms;
using System.Windows.Interop;
using System.Windows.Forms.Integration;
using System.Windows.Input;

namespace WpfTestHarness
    public partial class CustomShortcutHandlingDemo : Window
        public CustomShortcutHandlingDemo()

            // Registering against a stack event will cause memory leak, please unregister this event when you are done with it.
            ComponentDispatcher.ThreadPreprocessMessage += ComponentDispatcher_ThreadPreprocessMessage;

        const int WM_KEYDOWN = 0x100;
        const int WM_SYSKEYDOWN = 0x0104;
        private void ComponentDispatcher_ThreadPreprocessMessage(ref MSG msg, ref bool handled)
            if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
                // Examine if the Control and Enter keys are pressed, we also need to make sure that the
                // currently keyborad focused element is TextBox instance itself.
                swf.Keys keyData = ((swf.Keys)((int)((long)msg.wParam))) | swf.Control.ModifierKeys;
                if (((keyData & swf.Keys.Control) == swf.Keys.Control) &&
                    ((keyData & swf.Keys.Enter) == swf.Keys.Enter) &&
                    Keyboard.FocusedElement == textBox)
                    MessageBox.Show('Ctrl+Enter is pressed');

You can change the background of the grid layout when one of its children gets keyboard focus by setting the IsKeyboardFocusWithin property. This can be done with the following code snippet,

        <Style x:Key = "GridTriggerStyle" TargetType = "Grid">
                <Trigger Property = "IsKeyboardFocusWithin" Value = "True">
                    <Setter Property = "Background" Value = "Green" />
                <Trigger Property = "IsKeyboardFocusWithin" Value = "False">
                    <Setter Property = "Background" Value = "AliceBlue" />
    <Grid Name="testGrid" Style="{StaticResource GridTriggerStyle}">
        <Button Height="30" Width="100" Content="SampleButton1"/>

WPF provides a library of common commands, which include

Sample code given below:


<Window x:Class='SDKSamples.Window1'
      <Menu DockPanel.Dock='Top'>
        <MenuItem Command='ApplicationCommands.Paste' Width='75' />
      <TextBox BorderBrush='Black' BorderThickness='2' Margin='25'
        The MenuItem will not be enabled until
        this TextBox gets keyboard focus  

Share with

Couldn't find the FAQs you're looking for?

Please submit your question and answer.