/// <summary> /// When overridden in a derived class, is invoked whenever application code or internal processes call <see cref="M:System.Windows.FrameworkElement.ApplyTemplate"/>. /// </summary> public override void OnApplyTemplate() { this.UnSubscribeEvents(); this.isFirstTime = true; if (this.DropDownPopup != null) { this.DropDownPopup.KeyUp -= this.OnDropDownPopupKeyUp; } this.DropDownPopup = this.Template.FindName("PART_Popup", this) as Popup; if (this.DropDownPopup != null) { KeyboardNavigation.SetDirectionalNavigation(this.DropDownPopup, KeyboardNavigationMode.Cycle); KeyboardNavigation.SetTabNavigation(this.DropDownPopup, KeyboardNavigationMode.Continue); this.DropDownPopup.KeyUp += this.OnDropDownPopupKeyUp; } this.resizeVerticalThumb = this.Template.FindName("PART_ResizeVerticalThumb", this) as Thumb; this.resizeBothThumb = this.Template.FindName("PART_ResizeBothThumb", this) as Thumb; this.buttonBorder = this.Template.FindName("PART_ButtonBorder", this) as Border; this.menuPanel = this.Template.FindName("PART_MenuPanel", this) as MenuPanel; this.scrollViewer = this.Template.FindName("PART_ScrollViewer", this) as ScrollViewer; base.OnApplyTemplate(); this.SubscribeEvents(); }
/// <summary> /// When overridden in a derived class, positions child elements and /// determines a size for a System.Windows.FrameworkElement derived class. /// </summary> /// <param name="finalSize">The final area within the parent that this element /// should use to arrange itself and its children.</param> /// <returns>The actual size used.</returns> protected override Size ArrangeOverride(Size finalSize) { double totalHeight = 0; List <UIElement> nonItemsElements = new List <UIElement>(); foreach (var child in InternalChildren) { MenuItem item = child as MenuItem; if ((item != null) && (item.Visibility != Visibility.Collapsed)) { totalHeight += item.DesiredSize.Height; } else { Separator separator = child as Separator; if ((separator != null) && (separator.Visibility != Visibility.Collapsed)) { totalHeight += separator.DesiredSize.Height; } else { MenuPanel panel = child as MenuPanel; if (panel != null) { totalHeight += panel.DesiredSize.Height; } else { nonItemsElements.Add(child as UIElement); } } } } double y = 0; double deltaHeight = Math.Max(0, (finalSize.Height - totalHeight) / nonItemsElements.Count); foreach (var child in InternalChildren) { UIElement element = (child as UIElement); if ((element != null) && (element.Visibility != Visibility.Collapsed)) { double height = deltaHeight; if ((element is MenuItem) || (element is Separator) || (element is MenuPanel)) { height = element.DesiredSize.Height; } element.Arrange(new Rect(0, y, finalSize.Width, height)); y += height; } } return(finalSize); }
/// <summary> /// Called when the template's tree is generated. /// </summary> public override void OnApplyTemplate() { if (popup != null) { popup.Opened -= OnDropDownOpened; popup.Closed -= OnDropDownClosed; } popup = GetTemplateChild("PART_Popup") as Popup; if (popup != null) { popup.Opened += OnDropDownOpened; popup.Closed += OnDropDownClosed; KeyboardNavigation.SetControlTabNavigation(popup, KeyboardNavigationMode.Cycle); KeyboardNavigation.SetDirectionalNavigation(popup, KeyboardNavigationMode.Cycle); KeyboardNavigation.SetTabNavigation(popup, KeyboardNavigationMode.Cycle); } if (resizeVerticalThumb != null) { resizeVerticalThumb.DragDelta -= OnResizeVerticalDelta; } resizeVerticalThumb = GetTemplateChild("PART_ResizeVerticalThumb") as Thumb; if (resizeVerticalThumb != null) { resizeVerticalThumb.DragDelta += OnResizeVerticalDelta; } if (resizeBothThumb != null) { resizeBothThumb.DragDelta -= OnResizeBothDelta; } resizeBothThumb = GetTemplateChild("PART_ResizeBothThumb") as Thumb; if (resizeBothThumb != null) { resizeBothThumb.DragDelta += OnResizeBothDelta; } scrollViewer = GetTemplateChild("PART_ScrollViewer") as ScrollViewer; menuPanel = GetTemplateChild("PART_MenuPanel") as MenuPanel; }