// When visibility is changed, set the default focus void OnVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { if (!(bool)e.NewValue) { return; } var popupControl = VisualHelpers.FindChild <ContentControl>(this, "PopupContentControl"); Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(delegate { // Verify object really is visible because sometimes it's not once we switch to Render if (!GetIsPopupVisible(this)) { return; } if (lastFocusControl != null && lastFocusControl.Focusable) { lastFocusControl.Focus(); } else { lastFocusControl = VisualHelpers.FindChild <UIElement>(popupControl, "PART_DefaultFocusControl"); // If we can find the part named PART_DefaultFocusControl, set focus to it if (lastFocusControl != null && lastFocusControl.Focusable) { lastFocusControl.Focus(); } else { lastFocusControl = VisualHelpers.FindFirstFocusableChild(popupControl); // If no DefaultFocusControl found, try and set focus to the first focusable element found in popup if (lastFocusControl != null) { lastFocusControl.Focus(); } else { // Just give the Popup UserControl focus so it can handle keyboard input popupControl.Focus(); } } } } ) ); }
// Run storyboard when IsPopupVisible property changes to true void OnIsPopupVisibleChanged() { var isShown = GetIsPopupVisible(this); if (isShown && !isLoading) { var panel = VisualHelpers.FindChild <FrameworkElement>(this, "PopupPanelContent"); if (panel != null) { // Run Storyboard var animation = (Storyboard)panel.FindResource("ShowEditPanelStoryboard"); animation.Begin(); } } // When hiding popup, clear the LastFocusControl if (!isShown) { lastFocusControl = null; } }