Lostfocus actingas gotfocus for combobox in wpf - wpf

I'm using a combobox for which I have Lostfocus event set. But the lostfocus event is fired even when the combobox gets focus i.e gotfocus. Why is it happening so? If that is the default behavior is there any alternative solution for this?

From MSDN UIElement.LostFocus Event :
Because this event uses bubbling routing, the element that loses focus might be a child element instead of the element where the event handler is actually attached. Check the Source in the event data to determine the actual element that gained focus.
You can also use the IsFocused property of your ComboBox check if it has lost focus or not.

Related

WPF: Prevent Combobox from Swallowing Keydown Event

So I have a window that handles the KeyDown event. Everything works as expected except under two conditions:
The up / down arrow keys are pressed and
A combo box on the window has more than one item.
Even if I've never clicked on the combo box it doesn't seem to matter. The SelectionChanged event on the combobox fires before the Window even fires its KeyDown event. This seems highly counter-intuitive to me.
I don't know enough about WPF event propagation to even know where to start looking for a solution. Any recommendations?
You should subscribe to PreviewKeyDown event instead.

WPF: Controlling focus in a custom control

I am developing a custom control (say BoxControl) which will have many controls in it like a textbox, few buttons etc.
I will have many BoxControls in a row and while navigating via tabs, I want it to behave like when a BoxControl gets focus, it always passes the focus to its textbox and when its textbox loses the focus, the entire BoxControl loses the focu and passes the focus to next BoxControl.
Any ideas how can it be done?
You will need to add an event handler to your BoxControl to handle the GotFocus event and then put the focus on its text box.
You will need also to add an event handler to the LostFocus event of the textbox and then you can raise a custom event on BoxControl so it's controller can know that has to pass the focus to the next BoxControl
Hope it helps.
You can set Focusable property to false by a Setter in your custom control's template. Both on the control and on the various elements inside.

Why does TextBox Leave fire when mouse leaves?

I thought that Leave was supposed to fire when a control loses focus, and MouseLeave was supposed to fire when the mouse is no longer in the control.
I have a TextBox, and if I click in it, then take the mouse out, the Leave event fires. I'm using Leave to validate the entry in the box, like when people hit tab to go to the next control.
Does this mean that a TextBox can't have focus unless the mouse remains in it?
You must have some other code setting focus, because Leave does not fire when the mouse moves out of the control.
You should not be using Leave or LostFocus for validation purposes, instead use TextBox.Validating which is designed specifically for validation scenarios.
This way, if you want to have a Cancel button for example, you can just set its CausesValidation property to false and editor controls Validating events will not fire.
It doesn't matter if the mouse is in it. Property Focus refers to another type of event, when a component is ready to have its value changed, It doesn't matter if you use the keyboard or the mouse. Why don't you validate TextBox value when the Textbox looses Focus?

Silverlight: Event Handler Issue

So I have a Grid on a page that has a few UserControls on it. Each UserControl has a MouseLeftButtonDown event registered, as does the Grid. Before i added the event to the grid, the events on the user controls worked fine. But now that i have the event on the Grid, only the grid event fires regardless of where i click. None of the UseControls are capturing the event.
What do i need to do to allow the MouseLeftButtonDown events on the UserControls to fire while still having the MouseLeftButtonDown event on the Grid?
What you describe is very unusual.
What would often happen in this case it that both events fire. Since MouseLeftButtonDown is a bubbling event when you click on a UserControl it fires its MouseLeftButtonDown, if the handler attached to it doesn't set the Handled property of the MouseButtonEventArgs parameter to True then the event will bubble up to the parent and so on. If the parent controls also have code attached to their MouseLeftButtonDown events that code will also run.
Are sure that in fact the UserControl events don't fire or is that you happen to observe that the Grid event was always firing. If you are absolutely certain that attaching a event handler to the Grid actually prevents the UserControl events from firing can you edit your question with a small Repro, its very hard to see how this could be the case.

WPF what is the equivalent of SelectedIndexChanged for a datgrid combo box column?

How can I can an event to fire similar to selectedindexchanged with the datagridcombobox column type?
I would think that the recommended approach in this case is not to worry about the UI element firing the changed event, but rather listening for your ViewModel's change event which will be fired when the combo box changes the bound value.

Resources