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

  • Home
  • SEARCH
  • 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 6383625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:40:22+00:00 2026-05-25T02:40:22+00:00

I have an application in WPF that is to be used exclusively by the

  • 0

I have an application in WPF that is to be used exclusively by the keyboard, so we are really picky about focus behavior.

So, we get a PreviewLostKeyboardFocus on a textbox. Under certain circumstances we disable the next 5 fields and want the focus to go to the field after that. One might assume that the focus would do that, finding the next focusable field, this is what happens if I didn’t disable the fields in the preview event. It doesn’t, it keeps the focus on the first textbox.

I’ve tried forcing the focus with Keyboard.Focus(uielement) but nothing happens. It seems that the next focus target is already commited.

How can I make this happen, or am I “doing it wrong”? I am not in a position to change the requirement that specifies this behavior; I know that it is somewhat strange.

Thanks.

Edit: here is a small app that shows this behavior.
XAML:

<Window x:Class="WpfApplication4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    FocusManager.FocusedElement="{Binding ElementName=textBox0}"
    >
  <StackPanel>
    <TextBox Height="23" Margin="5" Name="textBox0" Width="120" />
    <TextBox Height="23" Margin="5" Name="textBox1" Width="120" PreviewLostKeyboardFocus="textBox1_PreviewLostKeyboardFocus"/>
    <TextBox Height="23" Margin="5" Name="textBox2" Width="120" />
    <TextBox Height="23" Margin="5" Name="textBox3" Width="120" />
    <TextBox Height="23" Margin="5" Name="textBox4" Width="120" />
  </StackPanel>
</Window>

Codebehind:

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

namespace WpfApplication4 {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
        }
        private void textBox1_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) {
            textBox2.IsEnabled = false;
            textBox3.IsEnabled = false;
        }
    }
}

An obvious thing to try (at least for me) was to put Keyboard.focus(textBox4); in the PreviewLostKeyboardFocus event handler. It of course didn’t work, it causes a loop that fires the PreviewLostKeyboardFocus event again….

Yet another edit:
I’ve found that using breakpoints in textBox1_PreviewLostKeyboardFocus() will sometimes cause it to behave, or sometimes not even disable the 2nd and 3rd text boxes. I’m thinking of a race/threading problem.

  • 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-05-25T02:40:23+00:00Added an answer on May 25, 2026 at 2:40 am

    I am not sure what exactly is causing this behavior but from past experience the WPF focus system is extremely unreliable after changing the controls in any way or when setting focus manually.

    However, using a Dispatcher to perform focus changes after WPF caught up with the changes to the controls, often solves the problem.

    This works fine in my testing

    private void textBox1_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) {
        textBox2.IsEnabled = false;
        textBox3.IsEnabled = false;
    
        this.Dispatcher
            .BeginInvoke(new Action(() => Keyboard.Focus(textBox4)), 
            System.Windows.Threading.DispatcherPriority.Input, null);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF application that's crashing once I get it onto machines that
hey, i've got a wpf application that have a doubleanimation used for scrolling text
In a MVVM WPF application, I have a master-detail View that is used to
I have a WPF application that has several modal window used for various purposes.
I have a WPF application that runs fine under XP as an administrator. When
We have a WPF Application that has a two flavors with a consistent UI
We have a WPF Application that runs from both desktop and as a XBAP
I have a WPF application that is a fullscreen kiosk app. It's actually a
I have a WPF application that will always run on windows 7, it opens
I have a wpf application (C#) that needs to copy a file to a

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.