BluePalmTree
BluePalmTree

Reputation: 333

Problems with Combobox MouseDown Events

I have two Problems with the Mouse Events of ComboBoxes. What I want to achieve is a "Touch-and-Release"-Solution, that means after the User pressed/touched the Combobox for 500ms something should happen. I have Class where I fireup all my Events to my Controls. For the Comboboxs I do it like this:

((ComboBox)obj).PreviewMouseDown -= CTRLMouseButtonEventHandler_Down;
                        ((ComboBox)obj).PreviewMouseDown += CTRLMouseButtonEventHandler_Down;
                        ((ComboBox)obj).PreviewMouseUp -= CTRLMouseButtonEventHandler_Up;
                        ((ComboBox)obj).PreviewMouseUp += CTRLMouseButtonEventHandler_Up;

My Up/Down-Events look like this:

private void CTRLMouseButtonEventHandler_Down(object sender, MouseEventArgs e)
    {
        _currentControl = (Control)sender;
        _touchHoldTimer = new System.Windows.Threading.DispatcherTimer();
        _touchHoldTimer.Interval = TimeSpan.FromMilliseconds(500);
        _touchHoldTimer.Tick += TouchHoldTimer_Tick;
        _touchHoldTimer.Start();           
    }

    private void CTRLMouseButtonEventHandler_Up(object sender, MouseEventArgs e)
    {
        _touchHoldTimer.Stop();
    }

The first Problem is, when my Focus is on another Control and click into the Combobox and hold, nothing happens. I first have to click into the Combobox and then click-and-hold and it works.

My second Problem is, that the PreviewMouseDown is also fired when the Scrollbar or ToggleButton of the Combobox is pressed. I tried something like this:

((ComboBox)obj).AddHandler(TextBox.PreviewMouseDownEvent, new RoutedEventHandler(CTRLMouseButtonEventHandler_Down2));
                        ((ComboBox)obj).AddHandler(TextBox.PreviewMouseUpEvent, new RoutedEventHandler(CTRLMouseButtonEventHandler_Up2));

But it didn' work. Can somebody point me in the reight direction please?

Upvotes: 1

Views: 1654

Answers (1)

Claudius
Claudius

Reputation: 1911

Add another event. Set Event for hovering mouse over Combobox to give it selection. It would be same as clicking on it.

EDIT:

other possible events:

DropDownOpened ContextMenuOpening

https://msdn.microsoft.com/en-us/library/system.windows.controls.combobox_events(v=vs.110).aspx

2. KeyDown event is not firing when pressing enter in an UserControl

Just replace enter with space.

Upvotes: 1

Related Questions