Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9005571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:10:53+00:00 2026-06-16T01:10:53+00:00

How can I set focus on a TextBox when F3 key pressed. In other

  • 0

How can I set focus on a TextBox when F3 key pressed.

In other words, do some thing like bellow:

private void Window_PreviewKeyDown(object sender, KeyEventArgs )
{
       switch (e.Key)
       {
           case Key.F3:
               myTextBox1.Focus();
               break;
           case Key.F4:
               myTextBox2.Focus();
               break;
           default:
               break;
      }
}

note:I want to do it in xaml.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:10:54+00:00Added an answer on June 16, 2026 at 1:10 am

    You could do this by created an attached property that takes a shortcut key then create an input binding on the window hosting that control.. it’s a bit of a complex class but very easy to use.

    start by adding a new class to your project below.

     public class TextBoxHelper : DependencyObject
        {
            public class MvvmCommand : DependencyObject, ICommand
            {
                readonly Action<object> _execute;
                readonly Func<object, bool> _canExecute;
                public event EventHandler CanExecuteChanged;
                public MvvmCommand(Action<object> execute, Func<object, bool> canExecute = null)
                {
                    if (execute == null) throw new ArgumentNullException("command");
                    _canExecute = canExecute == null ? parmeter => MvvmCommand.AlwaysCanExecute() : canExecute;
                    _execute = execute;
                }
                public object Tag
                {
                    get { return (object)GetValue(TagProperty); }
                    set { SetValue(TagProperty, value); }
                }
                public static readonly DependencyProperty TagProperty = DependencyProperty.Register("Tag", typeof(object), typeof(MvvmCommand), new PropertyMetadata(null));
                static bool AlwaysCanExecute()
                {
                    return true;
                }
                public void EvaluateCanExecute()
                {
                    EventHandler temp = CanExecuteChanged;
                    if (temp != null)
                        temp(this, EventArgs.Empty);
                }
                public virtual void Execute(object parameter)
                {
                    _execute(parameter == null ? this : parameter);
                }
                public virtual bool CanExecute(object parameter)
                {
                    return _canExecute == null ? true : _canExecute(parameter);
                }
            }
    
            public static Key GetFocusKey(DependencyObject obj)
            {
                return (Key)obj.GetValue(FocusKeyProperty);
            }
    
            public static void SetFocusKey(DependencyObject obj, Key value)
            {
                obj.SetValue(FocusKeyProperty, value);
            }
    
            public static readonly DependencyProperty FocusKeyProperty =
                DependencyProperty.RegisterAttached("FocusKey", typeof(Key), typeof(TextBoxHelper), new PropertyMetadata(Key.None, new PropertyChangedCallback((s, e) =>
                    {
                        UIElement targetElement = s as UIElement;
                        if (targetElement != null)
                        {
                            MvvmCommand command = new MvvmCommand(parameter => TextBoxHelper.FocusCommand(parameter))
                                {
                                    Tag = targetElement, 
                                };
                            InputGesture inputg = new KeyGesture((Key)e.NewValue);
                            (Window.GetWindow(targetElement)).InputBindings.Add(new InputBinding(command, inputg));
                        }
                    })));
    
            public static void FocusCommand(object parameter)
            {
                MvvmCommand targetCommand = parameter as MvvmCommand;
                if (targetCommand != null)
                {
                    UIElement targetElement = targetCommand.Tag as UIElement;
                    if (targetElement != null)
                    {
                        targetElement.Focus();
                    }
                }
            }
        }
    

    now in XAML all you need to do to set your focus keys is assign that FocusKey property, an example below has 2 textboxes, one gets focus when F1 is pressed, the other when F7 is pressed.

    <Window x:Class="WpfApplication5.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication5"
            DataContext="{Binding RelativeSource={RelativeSource Self}}"
            Title="MainWindow" Height="131" Width="460">
    
        <Grid Margin="10">
            <TextBox Margin="0,0,0,60" local:TextBoxHelper.FocusKey="F1" />
            <TextBox Margin="0,35,0,0" local:TextBoxHelper.FocusKey="F7" />
        </Grid>
    </Window>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to set the focus on a textbox and/or control after clicking
How can i set focus on the first textbox control in a popup (modal)?
How can I set focus on Amount textbox on page load for the following
How can I set focus to a hyperlink in asp.net? My hyperlink is in
Is there a way by which we can set the focus to a particular
I can't seem to get input focus on a textbox when a tab page
I want to set focus on textbox in popup panel. I have Login popup
In my Silverlight application, I can't seem to bring focus to a TextBox control.
I can set the width and height of my canvas by setting the width
I can set the relationship between View Model and view through following DataContext syntax:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.