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

Traverse focus after selection

3 Answers 381 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Marcel
Top achievements
Rank 1
Marcel asked on 16 May 2015, 04:10 PM

Hello,

I have a question about traversing the focus when a selection is made in the RadComboBox.

I have the following requirement:

When the user enters the combobox, the drop down should automatically open (I set OpenDropDownOnFocus= true) , the user will select an item and confirm the selection by using the enter key. After that the item should be set, the drop down should be closed and the focus should automatically traverse to the next field. Travesersing focus to the next field should be done by the enter-key, I already implemented this for textboxes.

 

I have used below code with the regular WPF ComboBox, but this gave some unexpected behavior in the past.

Sometimes the warning was shown ("Couldn't move the focus.......) and the user was not able anymore to traverse the focus in the whole screen using the enter key, he needed to close the screen and open it again to re-enable this functionality.

 

public class CustomComboBox : ComboBox
{
    #region Fields
 
    private bool _focused = false;
 
    #endregion
 
    #region Methods
 
    protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
    {
        base.OnGotKeyboardFocus(e);
 
        _focused = true;
    }
 
    protected override void OnDropDownClosed(EventArgs e)
    {
        if (_focused)
        {
            var uie = this as UIElement;
 
            _focused = false;
        }
        else
        {
            base.OnDropDownClosed(e);
        }
    }
 
    protected override void OnKeyDown(KeyEventArgs e)
    {
        if ((e.Key == System.Windows.Input.Key.Enter))
        {
            base.OnKeyDown(e);
 
 
            var comboBoxItem = e.OriginalSource as FrameworkElement;
            var comboBox = ItemsControl.ItemsControlFromItemContainer(comboBoxItem) as ComboBox;
 
            if (comboBox != null)
                comboBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            else
            {
                LogManager.GetLog(this.GetType()).Warn("Couldn't move the focus to the next field, source: {0}.", e.OriginalSource);
            }
        }
        else
            base.OnKeyDown(e);
 
    }
    #endregion
}

 

 What is the best way to accomplish this, or is this functionality already in the RadComboBox?

 

Regards,

 

Marcel

3 Answers, 1 is accepted

Sort by
0
Nasko
Telerik team
answered on 20 May 2015, 02:31 PM
Hello Chris,

In order to move the focus from RadComboBox to another control as soon as a selection is made we suggest you to use the MoveFocus method. You could call it inside the SelectionChanged event of the ComboBox control instead of implementing a custom a logic for the KeyDown event- that event will fire as soon as a selection is made. Please, check the attached sample project that demonstrates the described above approach - it seems everything works as expected on our side. Give it a try and let us know if it worked for you.

Hopes this helps.

Regards,
Nasko
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Marcel
Top achievements
Rank 1
answered on 06 Jun 2015, 12:15 PM

Hello Nasko,

Thanks for your reply, I checked out the solution.

It works well when there is no item selected yet.

But when there is already an item selected the user cant comfirm the already selected item with enter and traverse to the next field.

How can I combine this solution with that functionality?

Regards,

 

Marcel

 

0
Accepted
Nasko
Telerik team
answered on 08 Jun 2015, 12:52 PM
Hello Marcel,

In order to achieve the desired behavior you need also to handle the PreviewKeyUp event - we suggest you to handle that event because the KeyDown event could fire too early while there is still no selected item. Inside the event you need to implement the following logic:
private void combo_PreviewKeyUp(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        this.combo.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
    }
}

We hope this will help you.

Regards,
Nasko
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
ComboBox
Asked by
Marcel
Top achievements
Rank 1
Answers by
Nasko
Telerik team
Marcel
Top achievements
Rank 1
Share this question
or