// Coerce content static object CoerceContent(DependencyObject d, object basevalue) { StatusBarItem item = (StatusBarItem)d; // if content is null returns value if ((basevalue == null) && (item.Value != null)) { return(item.Value); } return(basevalue); }
// Handles IsChecked changed private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { StatusBarItem item = d as StatusBarItem; item.CoerceValue(VisibilityProperty); if ((bool)e.NewValue) { item.RaiseChecked(); } else { item.RaiseUnchecked(); } }
// Creates menu private void RecreateMenu() { contextMenu.Items.Clear(); // Adding header separator contextMenu.Items.Add(new GroupSeparatorMenuItem()); RibbonControl.Bind(Ribbon.Localization, contextMenu.Items[0] as FrameworkElement, "CustomizeStatusBar", MenuItem.HeaderProperty, BindingMode.OneWay); for (int i = 0; i < Items.Count; i++) { StatusBarItem item = Items[i] as StatusBarItem; if (item != null) { item.Checked += OnItemChecked; item.Unchecked += OnItemUnchecked; contextMenu.Items.Add(new StatusBarMenuItem(item)); } else { contextMenu.Items.Add(new Separator()); } } UpdateSeparartorsVisibility(); }
/// <summary> /// Invoked when the <see cref="P:System.Windows.Controls.ItemsControl.Items"/> property changes. /// </summary> /// <param name="e">Information about the change.</param> protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { base.OnItemsChanged(e); switch (e.Action) { case NotifyCollectionChangedAction.Add: { for (int i = 0; i < e.NewItems.Count; i++) { StatusBarItem item = e.NewItems[i] as StatusBarItem; if (item != null) { item.Checked += OnItemChecked; item.Unchecked += OnItemUnchecked; contextMenu.Items.Insert(e.NewStartingIndex + i + 1, new StatusBarMenuItem(item)); } else { contextMenu.Items.Insert(e.NewStartingIndex + i + 1, new Separator()); } } break; } case NotifyCollectionChangedAction.Move: { for (int i = 0; i < e.NewItems.Count; i++) { object menuItem = contextMenu.Items[e.OldStartingIndex + 1]; contextMenu.Items.RemoveAt(e.OldStartingIndex + 1); contextMenu.Items.Insert(e.NewStartingIndex + i + 1, menuItem); } break; } case NotifyCollectionChangedAction.Remove: { for (int i = 0; i < e.OldItems.Count; i++) { StatusBarMenuItem menuItem = contextMenu.Items[e.OldStartingIndex + 1] as StatusBarMenuItem; if (menuItem != null) { menuItem.StatusBarItem.Checked += OnItemChecked; menuItem.StatusBarItem.Unchecked += OnItemUnchecked; } contextMenu.Items.RemoveAt(e.OldStartingIndex + 1); } break; } case NotifyCollectionChangedAction.Replace: { for (int i = 0; i < e.OldItems.Count; i++) { StatusBarMenuItem menuItem = contextMenu.Items[e.OldStartingIndex + 1] as StatusBarMenuItem; if (menuItem != null) { menuItem.StatusBarItem.Checked += OnItemChecked; menuItem.StatusBarItem.Unchecked += OnItemUnchecked; } contextMenu.Items.RemoveAt(e.OldStartingIndex + 1); } for (int i = 0; i < e.NewItems.Count; i++) { StatusBarItem item = e.NewItems[i] as StatusBarItem; if (item != null) { item.Checked += OnItemChecked; item.Unchecked += OnItemUnchecked; contextMenu.Items.Insert(e.NewStartingIndex + i + 1, new StatusBarMenuItem(item)); } else { contextMenu.Items.Insert(e.NewStartingIndex + i + 1, new Separator()); } } break; } case NotifyCollectionChangedAction.Reset: { RecreateMenu(); break; } } }
static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { StatusBarItem item = (StatusBarItem)d; item.CoerceValue(ContentProperty); }
/// <summary> /// Default constructor /// </summary> /// <param name="item">Ribbon Status Bar menu item</param> public StatusBarMenuItem(StatusBarItem item) { this.StatusBarItem = item; }