Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combobox select item only by mouse or Enter key

Tags:

combobox

wpf

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

like image 851
Lari13 Avatar asked Oct 11 '11 12:10

Lari13


People also ask

How do I select items in ComboBox?

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.

When user select the value from combo box we want to display the name of the selected value <UNK> event is used?

You can use "ComboBoxItem. PreviewMouseDown" event.

What event from this class fires when the user of the control chooses an item from the ComboBox?

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.


2 Answers

You should have this event handled on all the ComboBoxItems 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.

like image 110
WPF-it Avatar answered Oct 05 '22 17:10

WPF-it


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);
}
like image 40
Rachel Avatar answered Oct 05 '22 16:10

Rachel