Microsoft.Phone.Controls.ContextMenu.OnApplyTemplate C# (CSharp) Method

OnApplyTemplate() public method

Called when a new Template is applied.
public OnApplyTemplate ( ) : void
return void
        public override void OnApplyTemplate()
        {
            // Unhook from old Template
            if (null != _openingStoryboard)
            {
                foreach (Storyboard sb in _openingStoryboard)
                {
                    sb.Completed -= OnStoryboardCompleted;
                }
                _openingStoryboard.Clear();
            }
            _openingStoryboardPlaying = false;

            // Apply new template
            base.OnApplyTemplate();

            SetDefaultStyle();

            // Hook up to new template
            FrameworkElement templateRoot = VisualTreeHelper.GetChild(this, 0) as FrameworkElement;
            if (null != templateRoot)
            {
                foreach (VisualStateGroup group in VisualStateManager.GetVisualStateGroups(templateRoot))
                {
                    if (VisibilityGroupName == group.Name)
                    {
                        foreach (VisualState state in group.States)
                        {
                            if ((OpenVisibilityStateName == state.Name || OpenLandscapeVisibilityStateName == state.Name || OpenReversedVisibilityStateName == state.Name || OpenLandscapeReversedVisibilityStateName == state.Name) && (null != state.Storyboard))
                            {
                                _openingStoryboard.Add(state.Storyboard);
                                state.Storyboard.Completed += OnStoryboardCompleted;
                            }
                        }
                    }
                }
            }

            _outerPanel = GetTemplateChild("OuterPanel") as StackPanel;
            _innerGrid = GetTemplateChild("InnerGrid") as Grid;

            // Go to correct visual state(s)
            bool portrait = DesignerProperties.IsInDesignTool || _rootVisual.Orientation.IsPortrait();

            SetRenderTransform();

            if (IsOpen)
            {
                // Handles initial open (where OnOpened is called before OnApplyTemplate)
                if (null != _innerGrid)
                {
                    // if landscape to the full height. NOTE: device is rotated so use the ActualWidth
                    _innerGrid.MinHeight = portrait ? 0 : _rootVisual.ActualWidth;
                }

                UpdateVisualStates(true);
            }
        }