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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:55:37+00:00 2026-06-14T15:55:37+00:00

Basis: I am using the MVVM pattern or a subset of it. In my

  • 0

Basis: I am using the MVVM pattern or a subset of it.

In my main window I have a button that opens up a usercontrol with a new DataContext the function looks kinda like this:

public void SetUserControl()
{
     UCDatacontext = new UCViewModel(this);
     base.OnPropertyChanged("UCDatacontext");
     UCViewVisibilty = Visibility.Visible;
     UCDatacontext.IniFocus(); 
}

And then when I am done I close the usercontrol and Dispose of the DataContext. Now the problem I am having is that I can’t seem to get the focus setting to work properly, I have a Textbox in the usercontrol that I want to set focus to when the view becomes Visible. However on the first time that I attempt to set focus it only fills the text box with an unblinking caret, which after investigation leads me to believe that it is because the TB isn’t getting the Keyboard focus (only logical focus), however even after explicity setting the keyboard focus I still get the unblinking caret, and it is only after clicking in the TB that it is getting focus. The method I am using to set focus is similar to method described here.
If in the view I do some writeline debugging by printing out in the FocusSet Event for the textbox it does get set, however only on the first time I call SetUserControl(). If I call SetUserControl() again it does nothing, except making the View Visible but doesn’t trigger the Focus Set Event.
Below is the lines of code from the MainWindow:

<Grid Grid.ColumnSpan="5" Grid.RowSpan="5" Visibility="{Binding Path=UCViewVisibilty }" x:Name="UCGrid"   >
    <Grid.Effect>
        <DropShadowEffect  />
    </Grid.Effect>
    <View:UCView DataContext="{Binding Path=UCDatacontext}"   />    
</Grid>

And UserControl Grid:

<Grid > 
    <TextBox Uid="UCTB" localExtensions:FocusExtension.IsFocused="{Binding Path=UCTBFocus}" Height="23" HorizontalAlignment="Left" Margin="113,56,0,0" Name="UCTB" VerticalAlignment="Top" Width="165" Text="{Binding Path=UCTBContent, UpdateSourceTrigger=PropertyChanged}" GotFocus="UCTB_GotFocus" />
</Grid >

The Focus is set in the UserControlViewModel, and is set after the Usercontrol is rendered.

  • 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-14T15:55:38+00:00Added an answer on June 14, 2026 at 3:55 pm

    As it turns out, after fiddling around with the code, the reason why the focus wasn’t being set properly in the View was because the binding in the View Model was this:

        bool _tBfocus;
        public bool UCTBFocus
        {
            get { return _tBfocus; }
            set
            {
                _tBfocus= value;
                base.OnPropertyChanged("UCTBFocus");
    
           }
    

    instead of:

        bool _tBfocus;
        public bool UCTBFocus
        {
            get { return _tBfocus; }
            set
            {
                if (_tBfocus == value)
                    return;
                _tBfocus= value;
                base.OnPropertyChanged("UCTBFocus");
    
            }
        }
    

    After changing it everything worked fine :/ but if someone could explain to me why this annoyance I was having was caused by that I would be truly grateful 🙂

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

Sidebar

Related Questions

I'm using the MVVM pattern in my first WPF app and have a problem
I am building a WPF app that is based on the MVVM pattern (using
I have an application that speaks to a server on a regular basis using
I have a relatively straightforward hierarchical data master-detail WPF window, developed using MVVM, where
we have developed an intranet application with Silverlight 4 using MVVM with PRISM. Up
I'm implementing a WPF application using the MVVM pattern. The application is basically a
I have a MVVM-based Window with many controls, and my Model implements IDataErrorInfo .
New to MVVM, it's clicking into place but I seem to have a knowledge/concept
I'm using the article Model View View-Model (MVVM) in Silverlight as a basis to
I have an application all done in WPF, using MvvM/Prism/Unity and Remote as datasource.

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.