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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:38:42+00:00 2026-05-31T01:38:42+00:00

I have an MVVM setup with a mainwindow that contains a ContentControl. I set

  • 0

I have an MVVM setup with a mainwindow that contains a ContentControl.
I set this to a particular viewmodel which then maps to a view.
A view is a usercontrol.
I want to be able to set the default keyboard focus to a default element in the usercontrol(View) when it loads so the application can eventually be driven just by using up, down, left, right and enter.
Some of my failed attempts are setting

FocusManager.FocusedElement="{Binding ElementName=DefaultElement}"

in my content control tag. This sets the logical focus but not the keyboard focus

I’d rather keep the solution in xaml if possable but have tried placing the following in code behind.

Keyboard.Focus(DefaultElement);  

This does not work but if I popup a message box first it does. I’m a little confused as to why.

MessageBox.Show(Keyboard.FocusedElement.ToString());
Keyboard.Focus(DefaultElement);

EDIT::::
I just placed this in my onloaded event of my user control. It seems to work but can anyone see any issues that might arrise at this priority level. I.E a circumstance when the action will never run?

Dispatcher.BeginInvoke(
            DispatcherPriority.ContextIdle,
            new Action(delegate()
                {
                    Keyboard.Focus(DefaultElement);
                }));
  • 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-31T01:38:44+00:00Added an answer on May 31, 2026 at 1:38 am

    It seems that this wpf the you have to implement a workaround on a case by case basis. The solution that seemed to work best, most of the time for me was to insert the focus code inside the dispatcher when OnVisible was changed. This sets the focus not only when the View/Usercontrol loads but also if you a changing Views by way of Visibility. If you Hide and then Show a ContentControl that is mapped to your ViewModels then the Loaded event won’t fire and you’ll be forced to Mouse input, or tabbing (Not so good if you want to navigate your app with a remote control).
    VisibilityChanged will always fire however. This is what I ended up with for my listbox.

    private void ItemsFlowListBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue == true)
            {
                Dispatcher.BeginInvoke(
                DispatcherPriority.ContextIdle,
                new Action(delegate()
                {
                    ItemsFlowListBox.Focus();
                    ItemsFlowListBox.ScrollIntoView(ItemsFlowListBox.SelectedItem);
                }));
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MVVM setup that creates a View on my MainWindow. I am
I have a mvvm(model view viewmodel) silverlight application that has several views that need
Trying to develop using MVVM: I have this Csla.PropertyStatus control that is created in
I have a WPF/MVVM (using MVVM-Light) app setup with a ComboBox that is inside
I have an MVVM application that uses a listbox which is populated with images.
I have an MVVM application. In one of the ViewModels is the 'FindFilesCommand' which
i have a mvvm app that the main window is a tab control. i
I have a WPF app that is using the MVVM pattern. Hooking up buttons
Using the MVVM pattern creating WPF applications you have the ViewModel providing data to
I have an MVVM Silverlight 4 application that holds a list of modules (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.