Esempio n. 1
0
        // 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();
                        }
                    }
                }
            }
                                              )
                                   );
        }
Esempio n. 2
0
        // 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;
            }
        }
Esempio n. 3
0
        // Keyboard Events
        void OnPreviewKeyDown(object sender, KeyEventArgs e)
        {
            var popup = VisualHelpers.FindAncester <PopupPanel>((DependencyObject)sender);
            var cmd   = GetPopupEscapeKeyCommand(popup);

            switch (e.Key)
            {
            case Key.Escape:
                if (cmd != null && cmd.CanExecute(null))
                {
                    cmd.Execute(null);
                    e.Handled = true;
                }
                else
                {
                    // By default the Escape Key closes the popup when pressed
                    var expression = GetBindingExpression(IsPopupVisibleProperty);
                    if (expression != null)
                    {
                        var dataType = expression.DataItem.GetType();
                        dataType.GetProperties().Single(x => x.Name == expression.ParentBinding.Path.Path)
                        .SetValue(expression.DataItem, false, null);
                    }
                }
                break;

            case Key.Enter:
                // Don't want to run Enter command if focus is in a TextBox with AcceptsReturn = True
                if (
                    !(e.KeyboardDevice.FocusedElement is TextBox && ((TextBox)e.KeyboardDevice.FocusedElement).AcceptsReturn) &&
                    cmd != null && cmd.CanExecute(null))
                {
                    cmd.Execute(null);
                    e.Handled = true;
                }
                break;
            }
        }
Esempio n. 4
0
 static object CoercePopupParent(DependencyObject obj, object value)
 {
     // If PopupParent is null, return the Window object
     return(value ?? VisualHelpers.FindAncester <Window>(obj));
 }