Focus on TextBox when UserControl change Visibility Focus on TextBox when UserControl change Visibility wpf wpf

Focus on TextBox when UserControl change Visibility


Well, I solve in this way:

private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e){    if (this.Visibility == Visibility.Visible)    {        this.Dispatcher.BeginInvoke((Action)delegate        {            Keyboard.Focus(TextBlockInput);        }, DispatcherPriority.Render);    }}

I think that the problem was tha focus call into IsVisibleChanged event "scope"...right?


try

Keyboard.Focus(TextBlockInput);

see here for more details


Another possible workaround is instead of Visibility property use Opacity. In this case calling Focus() actually sets focus.