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.

[XAML]

<Window.CommandBindings>
  <CommandBinding Command='ApplicationCommands.Open'
                  Executed='OpenCmdExecuted'
                  CanExecute='OpenCmdCanExecute'/>
</Window.CommandBindings>
Permalink

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

[C#]
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()
        {
            InitializeComponent();

            // 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');
                }
            }
        }
    }
}
Permalink

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,

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

WPF provides a library of common commands, which include

Sample code given below:

[XAML]

<Window x:Class='SDKSamples.Window1'
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    Title='MenuItemCommandTask'>
    <DockPanel>
      <Menu DockPanel.Dock='Top'>
        <MenuItem Command='ApplicationCommands.Paste' Width='75' />
      </Menu>
      <TextBox BorderBrush='Black' BorderThickness='2' Margin='25'
               TextWrapping='Wrap'>
        The MenuItem will not be enabled until
        this TextBox gets keyboard focus  
      </TextBox>
    </DockPanel>
<Window>
Permalink

Share with

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

Please submit your question and answer.