xNejX
asked on
WPF combobox selectionchanged event only on mouse click?
Hi Guys,
I have a ComboBox in my WPF xaml with some predefined ComboBoxItems. The ComboBox has the IsEditable enabled, so you can type in your username or select one from the predefined dropdown list of existing users on this system.
When the user is selected I would like the focus to be transferred on the password field, but I'm having some difficulties with it since the combobox fires the selectionchanged event if i type in the first letter of a matching item in the ComboBoxItem or if I try to cycle through the predefined values using the up / down arrow keys. It's understandable that it does that since the selection of it has been changed, but I would like to know how could I filter my SelectionChanged event to pass the focus to my password input only if the user clicks on the item form the ComboBox, not if the item is autosuggested or cycled through with arrow keys. A good example of the user input field that I am trying to achieve is the Skype "Skype Name" component in the Skype Sign In screen.
WPF xaml code:
C# Code:
I've also tried the preview on key event returning the e.handled which worked fine, but it disabled the functionality of manually inputting a new value to the ComboBox:
Thank you for your help,
I have a ComboBox in my WPF xaml with some predefined ComboBoxItems. The ComboBox has the IsEditable enabled, so you can type in your username or select one from the predefined dropdown list of existing users on this system.
When the user is selected I would like the focus to be transferred on the password field, but I'm having some difficulties with it since the combobox fires the selectionchanged event if i type in the first letter of a matching item in the ComboBoxItem or if I try to cycle through the predefined values using the up / down arrow keys. It's understandable that it does that since the selection of it has been changed, but I would like to know how could I filter my SelectionChanged event to pass the focus to my password input only if the user clicks on the item form the ComboBox, not if the item is autosuggested or cycled through with arrow keys. A good example of the user input field that I am trying to achieve is the Skype "Skype Name" component in the Skype Sign In screen.
WPF xaml code:
<ComboBox IsEditable="True" IsReadOnly="False" HorizontalAlignment="Left" Name="UserInput" Grid.ColumnSpan="2" " StaysOpenOnEdit="True" SelectionChanged="UserInput_SelectionChanged">
<ComboBoxItem Content="test" />
<ComboBoxItem Content="abc" />
<ComboBoxItem Content="xyz" />
</ComboBox>
C# Code:
private void UserInput_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
PasswordInput.Focus();
}
I've also tried the preview on key event returning the e.handled which worked fine, but it disabled the functionality of manually inputting a new value to the ComboBox:
private void UserInput_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
e.Handled = true;
}
Thank you for your help,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
just perfect :)
What I would do is set the OnMouseDown event for the combo box itself and then set a class-wide static bool variable to true if the mouse button was pushed while the cursor was over the combo box.
Then, in the OnSelectionChanged event, I'd check to see if the mouse had been previously pushed down. If so, then I'd do the move to the password box.
Make sure to reset the bool value to false as appropriate. You might have to tinker around with it. Anyway, that's what I'd do. There might be a more elegant solution.