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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:24:30+00:00 2026-05-28T14:24:30+00:00

I have a requirement to focus on a specific textbox when a new view

  • 0

I have a requirement to focus on a specific textbox when a new view is loaded.

The solution was to add this line of code to the OnLoaded event for the view:

Dispatcher.BeginInvoke(() => { NameTextBox.Focus(); });

So this worked for one view, but not another. I spent some time debugging the problem and realized that the new view I was working on had a BusyIndicator that takes focus away from all controls since the BusyIndicator being set to true and false was occuring after the OnLoaded event.

So the solution is to call focus to the NameTextBox after my BusyIndicator has been set to false. My idea was to create a reusable BusyIndicator control that handles this extra work. However, I am having trouble doing this in MVVM.

I started by making a simple extension of the toolkit:BusyIndicator:

public class EnhancedBusyIndicator : BusyIndicator
{
    public UserControl ControlToFocusOn { get; set; }

    private bool _remoteFocusIsEnabled = false;
    public bool RemoteFocusIsEnabled
    {
        get
        {
            return _remoteFocusIsEnabled;
        }
        set
        {
            if (value == true)
                EnableRemoteFocus();
        }
    }

    private void EnableRemoteFocus()
    {
        if (ControlToFocusOn.IsNotNull())
            Dispatcher.BeginInvoke(() => { ControlToFocusOn.Focus(); });
        else
            throw new InvalidOperationException("ControlToFocusOn has not been set.");
    }

I added the control to my XAML file with no problem:

<my:EnhancedBusyIndicator
    ControlToFocusOn="{Binding ElementName=NameTextBox}"
    RemoteFocusIsEnabled="{Binding IsRemoteFocusEnabled}"
    IsBusy="{Binding IsDetailsBusyIndicatorActive}"
...
>    
...
    <my:myTextBox (this extends TextBox)
        x:Name="NameTextBox"
    ...
    />
...
</my:EnhancedBusyIndicator>

So the idea is when IsRemoteFocusEnabled is set to true in my ViewModel (which I do after I’ve set IsBusy to false in the ViewModel), focus will be set to NameTextBox. And if it works, others could use the EnhancedBusyIndicator and just bind to a different control and enable the focus appropriately in their own ViewModels, assuming their views have an intial BusyIndicator active.

However, I get this exception when the view is loaded:

Set property ‘foo.Controls.EnhancedBusyIndicator.ControlToFocusOn’ threw an exception. [Line: 45 Position: 26]

Will this solution I am attempting work? If so, what is wrong with what I have thus far (cannot set the ControlToFocusOn property)?


Update 1

I installed Visual Studio 10 Tools for Silverlight 5 and got a better error message when navigating to the new view. Now I gete this error message:

“System.ArgumentException: Object of type System.Windows.Data.Binding cannot be converted to type System.Windows.Controls.UserControl”

Also, I think I need to change the DataContext for this control. In the code-behind constructor, DataContext is set to my ViewModel. I tried adding a DataContext property to the EnhancedBusyIndicator, but that did not work:

<my:EnhancedBusyIndicator
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    ControlToFocusOn="{Binding ElementName=NameTextBox}"
    RemoteFocusIsEnabled="{Binding IsRemoteFocusEnabled}"
    IsBusy="{Binding IsDetailsBusyIndicatorActive}"
...
>

Update 2

I need to change UserControl to Control since I will be wanting to set focus to TextBox objects (which implement Control). However, this does not solve the issue.

  • 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-28T14:24:31+00:00Added an answer on May 28, 2026 at 2:24 pm

    Without a BusyIndicator present in the view, the common solution to solve the focus problem is to add the code

    Dispatcher.BeginInvoke(() => { ControlToFocusOn.Focus(); });
    

    to the Loaded event of the view. This actually works even with the BusyIndicator present; however, the BusyIndicator immediately takes focus away from the rest of the Silverlight controls. The solution is to invoke the Focus() method of the control after the BusyIndicator is not busy.

    I was able to solve it by making a control like this:

    public class EnhancedBusyIndicator : BusyIndicator
    {
        public EnhancedBusyIndicator()
        {
            Loaded += new RoutedEventHandler(EnhancedBusyIndicator_Loaded);
        }
    
        void EnhancedBusyIndicator_Loaded(object sender, RoutedEventArgs e)
        {
            AllowedToFocus = true;
        }
    
        private readonly DependencyProperty AllowedToFocusProperty = DependencyProperty.Register("AllowedToFocus", typeof(bool), typeof(EnhancedBusyIndicator), new PropertyMetadata(true));
    
        public bool AllowedToFocus
        {
            get { return (bool)GetValue(AllowedToFocusProperty); }
            set { SetValue(AllowedToFocusProperty, value); }
        }
    
        public readonly DependencyProperty ControlToFocusOnProperty = DependencyProperty.Register("ControlToFocusOn", typeof(Control), typeof(EnhancedBusyIndicator), null);
    
        public Control ControlToFocusOn
        {
            get { return (Control)GetValue(ControlToFocusOnProperty); }
            set { SetValue(ControlToFocusOnProperty, value); }
        }
    
        protected override void OnIsBusyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnIsBusyChanged(e);
            if (AllowedToFocus && !IsBusy)
            {
                Dispatcher.BeginInvoke(() => { ControlToFocusOn.Focus(); });
                AllowedToFocus = false;
            }
        }
    }
    

    To use it, replace the BusyIndicator tags in your xaml with the new EnhancedBusyIndicator and add the appropriate namespace.

    Add a new property, ControlToFocusOn inside the element, and bind it to an existing element in the view that you want focus to be on after the EnhancedBusyIndicator disappears:

    <my:EnhancedBusyIndicator
        ControlToFocusOn="{Binding ElementName=NameTextBox}"
        ...
    >
        ...
    </my:EnhancedBusyIndicator>
    

    In this case, I focused to a textbox called NameTextBox.

    That’s it. This control will get focus every time we navigate to the page. While we are on the page, if the EnhancedBusyIndicator becomes busy and not busy agiain, focus will not go to the control; this only happens on initial load.

    If you want to allow the EnhancedBusyIndicator to focus to the ControlToFocusOn another time, add another property, AllowedToFocus:

    <my:EnhancedBusyIndicator
        ControlToFocusOn="{Binding ElementName=NameTextBox}"
        AllowedToFocus="{Binding IsAllowedToFocus}"
        ...
    >
        ...
    </my:EnhancedBusyIndicator>
    

    When AllowedToFocus is set to true, the next time EnhancedBusyIndicator switches from busy to not busy, focus will go to ControlToFocusOn.

    AllowedToFocus can also be set to false when loading the view, to prevent focus from going to a control. If you bind AllowedToFocus to a ViewModel property, you may need to change the BindingMode. By default, it is OneTime.

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

Sidebar

Related Questions

I have a requirement to set the focus to a textbox in a page
I have a requirement to make a large amount of code MISRA compliant. First
I have a requirement in Javascript (using Prototype) to focus the cursor on the
I have requirement to build new rule engine in which i have rule like
I have requirement like this, Have One NSIMageView and which will be changing frequently
I have requirement of specifying web part connections in onet.xml. So when site is
I have requirement like, suppose I have a 'property' table which has 'ListingKey' field
In our project we have requirement that, after receiving sms message from third party
We have a requirement in project to store all the revisions(Change History) for the
We have a requirement to increase the functionality of a grid we are using

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.