Unable to programmatically focus

3 posts, 0 answers
  1. Rolf
    Rolf avatar
    17 posts
    Member since:
    Nov 2013

    Posted 13 Nov Link to this post

    Hi,
    I'm using 2017.3.913.45 and I've been trying to focus a NumericUpDown-Control when the user changes the current Tab of a RadTabControl.

    So I've got a RadTabControl:

    <telerik:RadTabControl ... SelectionChanged="TabControl_SelectionChanged" ...>
        ..
        <telerik:RadTabItem Name="MyTab">
            ..

            <telerik:RadNumericUpDown Name="MyNumericUpDown" ..></telerik:RadNumericUpDown>
             ..

        </telerik:RadTabItem>
        ..
    </telerik:RadTabControl>

     

    And a TabControl_SelectionChanged Method:

    private void TabControl_SelectionChanged(object sender, RadSelectionChangedEventArgs e)
    {
        try
        {
            if(e.AddedItems[0] == MyTab)
            {
                MyNumericUpDown.Focus();
                Keyboard.Focus(MyNumericUpDown);
            }
        }
        catch
        {
            System.Diagnostics.Debugger.Break();
        }
    }

     

    I initially only tried with MyNumericUpDown.Focus() which did not work, a colleague asked me to add the Keyboard.Focus(MyNumericUpDown) call, but that also does not work.

    What's the workflow here?

  2. Dinko
    Admin
    Dinko avatar
    462 posts

    Posted 16 Nov Link to this post

    Hi Rolf,

    Thank you for your interest in RadNumericUpDown control.

    Let me first elaborate on the default focus behavior of the RadTabItem content. Internally in code, we are setting focus to the first element placed in the content of the RadTabItem. Currently, this logic cannot be overridden. 

    As a workaround, you can use a dispatcher to focus the desired control. You also need to set the DispatcherPriority to ApplicationIdle. Check the following code snippet.
    this.Dispatcher.BeginInvoke(new Action(() =>
    {
        MyNumericUpDown.Focus();
     
    }),DispatcherPriority.ApplicationIdle);

    Regards,
    Dinko
    Progress Telerik
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
  3. Rolf
    Rolf avatar
    17 posts
    Member since:
    Nov 2013

    Posted 16 Nov Link to this post

    Hi Dinko,

    thanks for the reply, I replaced the code and it seems to work. However, strangely the two lines I had there first worked the last two days without me even realizing it. Seems arbitrary.

    Thanks!

Back to Top