PopupHelper is a simple wrapper type that helps abstract platform differences out of the Popup.
Example #1
0
        /// <summary>
        /// Builds the visual tree for the
        /// <see cref="T:Microsoft.Phone.Controls.AutoCompleteBox" /> control
        /// when a new template is applied.
        /// </summary>
        public override void OnApplyTemplate()
        {
            if (TextBox != null)
            {
#if SILVERLIGHT
#if WINDOWS_PHONE
                TextBox.RemoveHandler(UIElement.KeyDownEvent, new KeyEventHandler(OnUIElementKeyDown));
                TextBox.RemoveHandler(UIElement.KeyUpEvent, new KeyEventHandler(OnUIElementKeyUp));
#else
                TextBox.RemoveHandler(TextBox.TextInputStartEvent, new TextCompositionEventHandler(OnTextBoxTextInputStart));
                TextBox.RemoveHandler(TextBox.TextInputEvent, new TextCompositionEventHandler(OnTextBoxTextInput));
#endif
#else
                TextBox.PreviewKeyDown -= OnTextBoxPreviewKeyDown;
#endif
            }

            if (DropDownPopup != null)
            {
                DropDownPopup.Closed -= DropDownPopup_Closed;
                DropDownPopup.FocusChanged -= OnDropDownFocusChanged;
                DropDownPopup.UpdateVisualStates -= OnDropDownPopupUpdateVisualStates;
                DropDownPopup.BeforeOnApplyTemplate();
                DropDownPopup = null;
            }

            base.OnApplyTemplate();

            // Set the template parts. Individual part setters remove and add 
            // any event handlers.
            Popup popup = GetTemplateChild(ElementPopup) as Popup;
            if (popup != null)
            {
                DropDownPopup = new PopupHelper(this, popup);
                DropDownPopup.MaxDropDownHeight = MaxDropDownHeight;
                DropDownPopup.AfterOnApplyTemplate();
                DropDownPopup.Closed += DropDownPopup_Closed;
                DropDownPopup.FocusChanged += OnDropDownFocusChanged;
                DropDownPopup.UpdateVisualStates += OnDropDownPopupUpdateVisualStates;

#if WP7
                // In WP7, a rendering bug caused a ScrollViewer inside a Popup in the visual tree
                // to be rendered at the wrong location onscreen.
                // We want to remove the popup from the visual tree so we can position it ourselves,
                // since having it in the visual tree doesn't work with any control that sets
                // a TranslateTransform on its children.
                DependencyObject popupParent = VisualTreeHelper.GetParent(popup);
                Panel popupParentPanel = popupParent as Panel;

                if (popupParentPanel != null)
                {
                    popupParentPanel.Children.Remove(popup);
                }
                else
                {
                    ContentControl popupParentContentControl = popupParent as ContentControl;

                    if (popupParentContentControl != null)
                    {
                        popupParentContentControl.Content = null;
                    }
                }
#endif
            }
            SelectionAdapter = GetSelectionAdapterPart();
            TextBox = GetTemplateChild(AutoCompleteBox.ElementTextBox) as TextBox;
            if (TextBox != null)
            {
#if SILVERLIGHT
#if WINDOWS_PHONE
                TextBox.AddHandler(UIElement.KeyDownEvent, new KeyEventHandler(OnUIElementKeyDown), true);
                TextBox.AddHandler(UIElement.KeyUpEvent, new KeyEventHandler(OnUIElementKeyUp), true);
#else
                TextBox.AddHandler(TextBox.TextInputStartEvent, new TextCompositionEventHandler(OnTextBoxTextInputStart), true);
                TextBox.AddHandler(TextBox.TextInputEvent, new TextCompositionEventHandler(OnTextBoxTextInput), true);
#endif
#else
                TextBox.PreviewKeyDown += OnTextBoxPreviewKeyDown;
#endif
            }

            Interaction.OnApplyTemplateBase();

            // If the drop down property indicates that the popup is open,
            // flip its value to invoke the changed handler.
            if (IsDropDownOpen && DropDownPopup != null && !DropDownPopup.IsOpen)
            {
                OpeningDropDown(false);
            }
        }
        /// <summary>
        /// Builds the visual tree for the
        /// <see cref="T:System.Windows.Controls.AutoCompleteBox" /> control
        /// when a new template is applied.
        /// </summary>
        public override void OnApplyTemplate()
        {
#if !SILVERLIGHT
            if (TextBox != null)
            {
                TextBox.PreviewKeyDown -= OnTextBoxPreviewKeyDown;
            }
#endif
            if (DropDownPopup != null)
            {
                DropDownPopup.Closed -= DropDownPopup_Closed;
                DropDownPopup.FocusChanged -= OnDropDownFocusChanged;
                DropDownPopup.UpdateVisualStates -= OnDropDownPopupUpdateVisualStates;
                DropDownPopup.BeforeOnApplyTemplate();
                DropDownPopup = null;
            }

            base.OnApplyTemplate();

            // Set the template parts. Individual part setters remove and add 
            // any event handlers.
            Popup popup = GetTemplateChild(ElementPopup) as Popup;
            if (popup != null)
            {
                DropDownPopup = new PopupHelper(this, popup);
                DropDownPopup.MaxDropDownHeight = MaxDropDownHeight;
                DropDownPopup.AfterOnApplyTemplate();
                DropDownPopup.Closed += DropDownPopup_Closed;
                DropDownPopup.FocusChanged += OnDropDownFocusChanged;
                DropDownPopup.UpdateVisualStates += OnDropDownPopupUpdateVisualStates;
            }
            SelectionAdapter = GetSelectionAdapterPart();
            TextBox = GetTemplateChild(AutoCompleteBox.ElementTextBox) as TextBox;
#if !SILVERLIGHT
            if (TextBox != null)
            {
                TextBox.PreviewKeyDown += OnTextBoxPreviewKeyDown;
            }
#endif
            Interaction.OnApplyTemplateBase();

            // If the drop down property indicates that the popup is open,
            // flip its value to invoke the changed handler.
            if (IsDropDownOpen && DropDownPopup != null && !DropDownPopup.IsOpen)
            {
                OpeningDropDown(false);
            }
        }
        /// <summary>
        /// Builds the visual tree for the
        /// <see cref="T:Microsoft.Phone.Controls.AutoCompleteBox" /> control
        /// when a new template is applied.
        /// </summary>
        public override void OnApplyTemplate()
        {
            if (TextBox != null)
            {
#if SILVERLIGHT
#if WINDOWS_PHONE
                TextBox.RemoveHandler(UIElement.KeyDownEvent, new KeyEventHandler(OnUIElementKeyDown));
                TextBox.RemoveHandler(UIElement.KeyUpEvent, new KeyEventHandler(OnUIElementKeyUp));
#else
                TextBox.RemoveHandler(TextBox.TextInputStartEvent, new TextCompositionEventHandler(OnTextBoxTextInputStart));
                TextBox.RemoveHandler(TextBox.TextInputEvent, new TextCompositionEventHandler(OnTextBoxTextInput));
#endif
#else
                TextBox.PreviewKeyDown -= OnTextBoxPreviewKeyDown;
#endif
            }

            if (DropDownPopup != null)
            {
                DropDownPopup.Closed -= DropDownPopup_Closed;
                DropDownPopup.FocusChanged -= OnDropDownFocusChanged;
                DropDownPopup.UpdateVisualStates -= OnDropDownPopupUpdateVisualStates;
                DropDownPopup.BeforeOnApplyTemplate();
                DropDownPopup = null;
            }

            base.OnApplyTemplate();

            // Set the template parts. Individual part setters remove and add 
            // any event handlers.
            Popup popup = GetTemplateChild(ElementPopup) as Popup;
            if (popup != null)
            {
                DropDownPopup = new PopupHelper(this, popup);
                DropDownPopup.MaxDropDownHeight = MaxDropDownHeight;
                DropDownPopup.AfterOnApplyTemplate();
                DropDownPopup.Closed += DropDownPopup_Closed;
                DropDownPopup.FocusChanged += OnDropDownFocusChanged;
                DropDownPopup.UpdateVisualStates += OnDropDownPopupUpdateVisualStates;
            }
            SelectionAdapter = GetSelectionAdapterPart();
            TextBox = GetTemplateChild(AutoCompleteBox.ElementTextBox) as TextBox;
            if (TextBox != null)
            {
#if SILVERLIGHT
#if WINDOWS_PHONE
                TextBox.AddHandler(UIElement.KeyDownEvent, new KeyEventHandler(OnUIElementKeyDown), true);
                TextBox.AddHandler(UIElement.KeyUpEvent, new KeyEventHandler(OnUIElementKeyUp), true);
#else
                TextBox.AddHandler(TextBox.TextInputStartEvent, new TextCompositionEventHandler(OnTextBoxTextInputStart), true);
                TextBox.AddHandler(TextBox.TextInputEvent, new TextCompositionEventHandler(OnTextBoxTextInput), true);
#endif
#else
                TextBox.PreviewKeyDown += OnTextBoxPreviewKeyDown;
#endif
            }

            Interaction.OnApplyTemplateBase();

            // If the drop down property indicates that the popup is open,
            // flip its value to invoke the changed handler.
            if (IsDropDownOpen && DropDownPopup != null && !DropDownPopup.IsOpen)
            {
                OpeningDropDown(false);
            }
        }