I have a WPF ComboBox
I need to change default behaivor in popup list.
Now, by pressing up
and down
keys SelectedItem
changes automatically.
I need change SelectedItem
only by pressing Enter
key, or clicking by mouse.
How can it be done?
I've subclassed ComboBox
:
protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
{
Debug.Write("Pressed " + e.Key+ " ");
if (e.Key == System.Windows.Input.Key.Up || e.Key == System.Windows.Input.Key.Down)
{
// ???
e.Handled = true;
return;
}
base.OnPreviewKeyDown(e);
}
This code doesn't work - no popup is shown and user can't select items. What I shoud write and where? :)
Thanks.
UPD1:
I need same functionality as ComboBox's
popup is open and user can select items by mouse.
Each item can be hovered by mouse, but not selected. Selection become only by pressing mouse button. I need the same. 'Up' and 'Down' only highlights items in popup, but SelectedItem
will be changed only by pressing Enter
or mouse clicking.
UPD2: If I press by mouse on button, that opens Popup in ComboCox, I can highlight items in Popup by mouse, but SelectedItem will change only if I click on item.
I need same functionality by keyboard. If I start typing somewhat in ComboBox, Popup opens. And I have to hightlight items by keyboard Up
and Down
. TextBox in ComboBox must not change during highlighting and SelectedItem must change only if I press Enter
(or mouse click)
UPD3: Link to demo solution: download
When you set the SelectedItem property to an object, the ComboBox attempts to make that object the currently selected one in the list. If the object is found in the list, it is displayed in the edit portion of the ComboBox and the SelectedIndex property is set to the corresponding index.
You can use "ComboBoxItem. PreviewMouseDown" event.
The combo box fires an action event when the user selects an item from the combo box's menu. See How to Write an Action Listener, for general information about implementing action listeners.
You should have this event handled on all the ComboBoxItem
s in a combobox.
<ComboBox.Resources>
<Style TargetType="{x:Type ComboBoxItem}">
<EventSetter Event="PreviewKeyDown" Handler="OnPreviewKeyDown" />
</Style>
</ComboBox.Resources>
EDIT:
In code behind, you can add folowing code in MyComboBox's constructor after InitializeComponent()
do this...
var comboBoxItemstyle = new Style(typeof (ComboBoxItem));
comboBoxItemstyle.Setters.Add(
new EventSetter(PreviewKeyDownEvent,
new KeyEventHandler(OnPreviewKeyDown)));
this.Resources.Add(typeof (ComboBoxItem), comboBoxItemstyle);
Hope this helps.
The code you have seems to work fine, just add a check to see if the DropDown is open before cancelling the Key Event
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
Debug.Write("Pressed " + e.Key + " ");
if (!base.IsDropDownOpen && (e.Key == Key.Up || e.Key == Key.Down))
{
e.Handled = true;
return;
}
base.OnPreviewKeyDown(e);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With