This is a migrated thread and some comments may be shown as answers.

Set Keyboard Focus

4 Answers 755 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 28 Feb 2017, 11:06 PM
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? 

4 Answers, 1 is accepted

Sort by
0
Stefan Nenchev
Telerik team
answered on 03 Mar 2017, 11:31 AM
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 you to write beautiful native mobile apps using a single shared C# codebase.
0
David
Top achievements
Rank 1
answered on 03 Mar 2017, 04:20 PM

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();
        }
    }
}
0
Accepted
Dilyan Traykov
Telerik team
answered on 08 Mar 2017, 02:36 PM
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 you to write beautiful native mobile apps using a single shared C# codebase.
0
David
Top achievements
Rank 1
answered on 13 Mar 2017, 04:00 AM
Works like a charm. Thanks!
Tags
ComboBox
Asked by
David
Top achievements
Rank 1
Answers by
Stefan Nenchev
Telerik team
David
Top achievements
Rank 1
Dilyan Traykov
Telerik team
Share this question
or