System.Windows.Controls.PopupHelper.AfterOnApplyTemplate C# (CSharp) Method

AfterOnApplyTemplate() public method

Should be called by the parent control after the base OnApplyTemplate method is called.
public AfterOnApplyTemplate ( ) : void
return void
        public void AfterOnApplyTemplate()
        {
            if(Popup != null)
            {
                Popup.Closed += Popup_Closed;
            }

            VisualStateGroup groupPopupClosed = VisualStates.TryGetVisualStateGroup(Parent, VisualStates.GroupPopup);
            if(null != groupPopupClosed)
            {
                groupPopupClosed.CurrentStateChanged += OnPopupClosedStateChanged;
                UsesClosingVisualState = true;
            }

            // TODO: Consider moving to the DropDownPopup setter
            // TODO: Although in line with other implementations, what happens 
            // when the template is swapped out?
            if(Popup != null)
            {
                PopupChild = Popup.Child as FrameworkElement;

                if(PopupChild != null)
                {
#if SILVERLIGHT
                    // For Silverlight only, we just create the popup child with 
                    // canvas a single time.
                    if (!_hasControlLoaded)
                    {
                        _hasControlLoaded = true;

                        // Replace the poup child with a canvas
                        PopupChildCanvas = new Canvas();
                        Popup.Child = PopupChildCanvas;

                        OutsidePopupCanvas = new Canvas();
                        OutsidePopupCanvas.Background = new SolidColorBrush(Colors.Transparent);
                        OutsidePopupCanvas.MouseLeftButtonDown += OutsidePopup_MouseLeftButtonDown;

                        PopupChildCanvas.Children.Add(OutsidePopupCanvas);
                        PopupChildCanvas.Children.Add(PopupChild);
                    }
#endif

                    PopupChild.GotFocus += PopupChild_GotFocus;
                    PopupChild.LostFocus += PopupChild_LostFocus;
                    PopupChild.MouseEnter += PopupChild_MouseEnter;
                    PopupChild.MouseLeave += PopupChild_MouseLeave;
                    PopupChild.SizeChanged += PopupChild_SizeChanged;
                }
            }
        }

Usage 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);
            }
        }
All Usage Examples Of System.Windows.Controls.PopupHelper::AfterOnApplyTemplate