/// <summary> /// Handle the MenuItem's Click event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void ContextMenuItem_Click(object sender, RoutedEventArgs e) { MenuItem mi = sender as MenuItem; if (mi == null) { return; } int index; // get the index of the TabItem from the manuitems Tag property bool b = int.TryParse(mi.Tag.ToString(), out index); if (b) { TabItem tabItem = this.Items[index] as TabItem; if (tabItem != null) { VirtualizingTabPanel itemsHost = Helper.FindVirtualizingTabPanel(this); if (itemsHost != null) { itemsHost.MakeVisible(tabItem, Rect.Empty); } tabItem.Focus(); if (TabItemSelected != null) { TabItemSelected(this, new TabItemEventArgs(tabItem)); } } } }
/// <summary> /// Find the Panel for the TabControl /// </summary> public static VirtualizingTabPanel FindVirtualizingTabPanel(Visual visual) { if (visual == null) { return(null); } for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++) { Visual child = VisualTreeHelper.GetChild(visual, i) as Visual; if (child != null) { if (child is VirtualizingTabPanel) { object temp = child; return((VirtualizingTabPanel)temp); } VirtualizingTabPanel panel = FindVirtualizingTabPanel(child); if (panel != null) { object temp = panel; return((VirtualizingTabPanel)temp); // return the panel up the call stack } } } return(null); }
/// <summary> /// OnMinMaxChanged callback responds to any of the Min/Max dependancy properties changing /// </summary> /// <param name="d"></param> /// <param name="e"></param> private static void OnMinMaxChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { TabControl tc = (TabControl)d; if (tc.Template == null) { return; } VirtualizingTabPanel tp = Helper.FindVirtualizingTabPanel(tc); if (tp != null) { tp.InvalidateMeasure(); } }
/* * Protected override methods * */ /// <summary> /// OnApplyTemplate override /// </summary> public override void OnApplyTemplate() { base.OnApplyTemplate(); // set up the event handler for the template parts _toggleButton = this.Template.FindName("PART_DropDown", this) as ToggleButton; if (_toggleButton != null) { // create a context menu for the togglebutton System.Windows.Controls.ContextMenu cm = new ContextMenu(); cm.PlacementTarget = _toggleButton; cm.Placement = PlacementMode.Bottom; // create a binding between the togglebutton's IsChecked Property // and the Context Menu's IsOpen Property Binding b = new Binding(); b.Source = _toggleButton; b.Mode = BindingMode.TwoWay; b.Path = new PropertyPath(ToggleButton.IsCheckedProperty); cm.SetBinding(ContextMenu.IsOpenProperty, b); _toggleButton.ContextMenu = cm; _toggleButton.Checked += DropdownButton_Checked; } ScrollViewer scrollViewer = this.Template.FindName("PART_ScrollViewer", this) as ScrollViewer; // set up event handlers for the RepeatButtons Click event RepeatButton repeatLeft = this.Template.FindName("PART_RepeatLeft", this) as RepeatButton; if (repeatLeft != null) { repeatLeft.Click += delegate { if (scrollViewer != null) { scrollViewer.LineLeft(); } GC.Collect(); }; } RepeatButton repeatRight = this.Template.FindName("PART_RepeatRight", this) as RepeatButton; if (repeatRight != null) { repeatRight.Click += delegate { if (scrollViewer != null) { scrollViewer.LineRight(); } GC.Collect(); }; } // set up the event handler for the 'New Tab' Button Click event ButtonBase button = this.Template.FindName("PART_NewTabButton", this) as ButtonBase; if (button != null) { button.Click += delegate { int i = this.SelectedIndex; TabItem item = new TabItem(); item.Header = "New Tab"; if (i == -1 || i == this.Items.Count - 1 || AddNewTabToEnd) { this.Items.Add(item); } else { this.Items.Insert(++i, item); } if (SelectNewTabOnCreate) { SelectedItem = item; VirtualizingTabPanel itemsHost = Helper.FindVirtualizingTabPanel(this); if (itemsHost != null) { itemsHost.MakeVisible(item, Rect.Empty); } item.Focus(); if (TabItemSelected != null) { TabItemSelected(this, new TabItemEventArgs(item)); } } if (TabItemAdded != null) { TabItemAdded(this, new TabItemEventArgs(item)); } }; } }