Set Keyboard Focus

5 posts, 1 answers
  1. David
    David avatar
    32 posts
    Member since:
    Nov 2015

    Posted 28 Feb Link to this post

    Client would like the keyboard cursor inside of the Combo Box when the Window loads. Calling "ComboBox.Focus()" does not place the keyboard cursor into the editable part of the ComboBox. Am I missing something simple? 
  2. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    503 posts

    Posted 03 Mar Link to this post

    Hello David,

    Calling the Focus method of the RadComboBox results in the cursor appearing if there is no SelectedItem set. Otherwise, the text within it will be selected and typing will override the value. Isn't this the behavior you are looking for? Please have a look at the sample. Do you see a different behavior in your setup? Please share more information on your scenario.

    Regards,
    Stefan Nenchev
    Telerik by Progress
    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. David
    David avatar
    32 posts
    Member since:
    Nov 2015

    Posted 03 Mar Link to this post

    Hi Stefan,

    Thanks for the reply. You are right. That is the behavior I'm looking for, however I am acquiring it through an Attached Property so I can control Focus through a View Model.

    When I set the Attached Property "IsFocused" to "True", the TextBox portion of the ComboBox does not focus.

    Here is the Attached Property for your reference.

    public class RadComboBoxProperties
    {
        public static DependencyProperty IsFocusedProperty = DependencyProperty.RegisterAttached("IsFocused", typeof( bool ), typeof( RadComboBoxProperties ), new UIPropertyMetadata( false, OnIsFocusedChanged ) );
      
        public static bool GetIsFocused( DependencyObject dependencyObject )
        {
            return (bool)dependencyObject.GetValue( IsFocusedProperty );
        }
      
        public static void SetIsFocused( DependencyObject dependencyObject, bool value )
        {
            dependencyObject.SetValue( IsFocusedProperty, value );
        }
      
        public static void OnIsFocusedChanged( DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs )
        {
            RadComboBox comboBox = dependencyObject as RadComboBox;
            bool newValue = (bool)dependencyPropertyChangedEventArgs.NewValue;
            bool oldValue = (bool)dependencyPropertyChangedEventArgs.OldValue;
            if( newValue && !oldValue && !comboBox.IsFocused )
            {
                comboBox.Focus();
            }
        }
    }
  4. Answer
    Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    603 posts

    Posted 08 Mar Link to this post

    Hello David,

    The reason the focus is not set to the RadComboBox properly is because when the Focus method is invoked, the control has not yet been fully loaded.

    You will need to use a Dispatcher for this to work. Here's an interesting thread discussing a similar issue.

    Therefore, you can define the OnIsFocusedChanged method like so:

    public static void OnIsFocusedChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        RadComboBox comboBox = dependencyObject as RadComboBox;
        bool newValue = (bool)dependencyPropertyChangedEventArgs.NewValue;
        bool oldValue = (bool)dependencyPropertyChangedEventArgs.OldValue;
        if (newValue && !oldValue && !comboBox.IsFocused)
        {
            comboBox.Dispatcher.BeginInvoke((Action)(() =>
            {
                comboBox.Focus();
            }), DispatcherPriority.Render);
        }
    }

    Do let me know if this works for you.

    Regards,
    Dilyan Traykov
    Telerik by Progress
    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.
  5. David
    David avatar
    32 posts
    Member since:
    Nov 2015

    Posted 12 Mar in reply to Dilyan Traykov Link to this post

    Works like a charm. Thanks!
Back to Top