tags:

views:

571

answers:

2

I have code that handles the LostFocus event of my controls. It validates the value and in some cases will enable a subsequent control. For instance, there might be a ComboBox that allows a user to select a country. The subsequent ComboBox allows the user to select a state. IF the currently selected country is not the USA, the state ComboBox is disabled. If the user selects "USA" and then tabs out of the combo box, the LostFocus code enables the state ComboBox. However, the State ComboBox does not get focus, instead focus goes to the control that follows the State ComboBox.

I've tried using the PreviewLostKeyboardFocus to handle the event instead with no luck. I'm kind of at a loss as to figuring out someway to hack WPF to get this work. Any suggestions?

A: 

I'm guessing what is happening is it determines the control to tab to before the LostFocus event fires, thereby skipping the State combo box since it is disabled. Here's the information for how focus works in WPF. What you will want to do is in your handler, determine if it should be going to the State combo box next, and programatically focus that element via the FocusManager class.

Darren Kopp
+1  A: 

try validating when the data changes, not the UI. You can add validation rules that will fire when the property is updated from the binding. Then you can use a style trigger to activate the control in question. Check this article it should help.

Muad'Dib