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.

Community
  • 1
  • 1
Heinrich
  • 2,144
  • 3
  • 23
  • 39

2 Answers2

0

it like this Set focus one by one from top to bottom.

InitializeComponent();
        FocusManager.SetFocusedElement(this, TabItem); //this is Window , TabItem is UserControl in this Window
        FocusManager.SetFocusedElement(TabItem, TextBox); // TabItem is UserControl and TextBox is Control in TabItem UC

I hope this will help.

yo chauhan
  • 12,079
  • 4
  • 39
  • 58
  • The problem still persists :/, for some reason the GotFocus event for the text box is only called on the first attempt. – Heinrich Nov 20 '12 at 20:13
0

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 :)

Heinrich
  • 2,144
  • 3
  • 23
  • 39