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 783935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:37:50+00:00 2026-05-14T20:37:50+00:00

I have been putting off activity on SO because my current reputation is 1337.

  • 0

I have been putting off activity on SO because my current reputation is “1337”. 🙂

This is a question of “why” and not “how”. By default, it seems that WPF does not set focus to the first control in a window when it’s opening. In addition, when a textbox gets focus, by default it does not have it’s existing text selected. So basically when I open a window, I want focus on the first control of the window, and if that control is a textbox, I want it’s existing text (if any) to be selected.

I found some tips online to accomplish each of these behaviors, and combined them. The code below, which I placed in the constructor of my window, is what I came up with:

Loaded += (sender, e) =>
          {
              MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
              var textBox = FocusManager.GetFocusedElement(this) as TextBox;
              if (textBox != null)
              {
                  Action select = textBox.SelectAll;
                  //for some reason this doesn't work without using invoke.
                  Dispatcher.Invoke(DispatcherPriority.Loaded, select);
              }
          };

So, my question. Why does the above not work without using Dispatcher.Invoke? Is something built into the behavior of the window (or textbox) cause the selected text to be de-selected post-loading?

Maybe related, maybe not–another example of where I had to use Dispatcher.Invoke to control the behavior of a form:

WPF Focus In Tab Control Content When New Tab is Created

  • 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-14T20:37:50+00:00Added an answer on May 14, 2026 at 8:37 pm

    All WPF controls have thread affinity. The Dispatcher manages the thread that each control was created on (typically this is a single thread for every control in the application, but not necessarily). Work is queued on this thread and executed in priority order.

    Any UI-manipulating code has to be executed on the same thread as the control was created on – the Dispatcher thread – and so any method has to invoke back to that thread before it can do anything that would affect the UI (such as selecting text in the TextBox).

    That said, it’s my understanding that the Loaded eventhandler would fire on the Dispatcher thread by default, so I’m not entirely sure why you’re seeing this behaviour in your specific example!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been putting off this question for some time but now it is
I have been putting off developing this part of my app for sometime purely
Have been working on this question for a couple hours and have come close
I have been putting only jQuery mask so far, but I figured out that
I am stuck with this, I have been putting myself through a crash course
ListViews have always been my weak point and right now I am practicing putting
I have been tasked with refactoring some components that used xmlbeans to now make
I have been putting them in the toplevel. I am wondering if it is
I have been trying to work out the bug on this but can't seem
I have several timers that poll information off the internet. When I get an

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.