I have a really big wpf application with many nested controls using caliburn micro and MVVM. One control needs a check a condition before(while) the user leaves. If the check fails the focus is transfered back to the said control.
Using Finding ALL child controls WPF I solved the problem with child control also firing the LostFocus event. And I used the Dispatcher to put the focus back to the control.
my code looks like this:
public void LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
var parent = ParentFinder.TryFindParent<ProfileFunctionView>(e.NewFocus as FrameworkElement);
//only fire for outside controls, not child controls
if (parent == null)
{
if (!Apply())
{
var restoreFocus = (System.Threading.ThreadStart)delegate { SyntaxEditor.Focus(); };
Application.Current.Dispatcher.BeginInvoke(restoreFocus);
//stuff should happen here
}
}
}
The problem is that, if I click on a tab control somewhere above my control f.e.. The focus is set back correctly, but than the tab changes. I want to prevent the controls that are clicked outside of my control to react if the condition fails.
Is this possible? Is my approach correct?
Please excuse my confusing title, this problem is really hard to formulate or google. Any help is appreciated.