private void TargetElement_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) { log.DebugFormat("Losing keyboard focus: {0}, from {1}, to {2}", sender, e.OldFocus, e.NewFocus); DependencyObject oldFocus = e.OldFocus as DependencyObject; DependencyObject newFocus = e.NewFocus as DependencyObject; if (oldFocus != null & newFocus != null) { if (TargetElement.IsAncestorOf(oldFocus) && TargetElement.IsAncestorOf(newFocus)) { // Focus moved within the container. return; } } log.Debug("Saving state on UI service providers"); var statePersistentServices = UIServiceProvider.GetAllServices <IStatePersistency>(TargetElement); foreach (var service in statePersistentServices) { service.SaveCurrentState(); } }
private void TargetElement_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) { log.DebugFormat("Regaining keyboard focus: {0}, from {1}, to {2}", sender, e.OldFocus, e.NewFocus); //if (isUpdatingFocus.IsSet) // return; if (isFirstFocus) { isFirstFocus = false; log.Debug("Got focus for the first time."); UpdateFocus(); return; } if (isRestoringState.IsSet) { return; } using (isRestoringState.Set()) { DependencyObject oldFocus = e.OldFocus as DependencyObject; if (oldFocus == null) { log.Debug("Old focus is null. Not restoring focus."); return; } DependencyObject newFocus = e.NewFocus as DependencyObject; if (newFocus != null) { if (TargetElement.IsAncestorOf(oldFocus) && TargetElement.IsAncestorOf(newFocus)) { log.DebugFormat("Focus changed within the same element"); // Focus moved within the container. return; } } log.Debug("Restoring state on UI service providers"); var statePersistentServices = UIServiceProvider.GetAllServices <IStatePersistency>(TargetElement); foreach (var service in statePersistentServices) { service.RestoreState(); } } }