123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using System.ComponentModel;
- using System.Windows.Controls;
- namespace CustomControls
- {
- [DesignTimeVisible(false)]
- public partial class ContextMenuStrip : ContextMenu
- {
- public ContextMenuStrip()
- {
- InitializeComponent();
- }
- public bool EnableQuickAccessToolbar { get; set; }
- public void SetIsOpen(bool value)
- {
- if (value && EnableQuickAccessToolbar)
- MakeToolbarItems();
- IsOpen = value;
- }
- private void RemoveDuplicateSeparators(ItemCollection items)
- {
- for (int i = 1; i < items.Count; i++)
- {
- var item = items[i];
- if (item is Separator)
- {
- // check if previous item is not separator
- var previousItem = items[i - 1];
- if (previousItem is Separator)
- {
- items.RemoveAt(i);
- i--;
- }
- }
- }
- }
- private void TrimSeparators(ItemCollection items)
- {
- if (items.Count > 0 && items[0] is Separator)
- items.RemoveAt(0);
- if (items.Count > 0 && items[items.Count - 1] is Separator)
- items.RemoveAt(items.Count - 1);
- }
- private ToolStripButton CreateToolbarButton(System.Windows.Forms.ToolStripMenuItem swfMenuItem)
- {
- var button = new ToolStripButton() { Width = 22, Height = 22 };
- button.Image = swfMenuItem.ImageSource;
- button.ToolTip = swfMenuItem.Text;
- button.IsEnabled = swfMenuItem.Enabled;
- button.Click += (s, e) =>
- {
- swfMenuItem.OnItemClicked();
- IsOpen = false;
- };
- return button;
- }
- private void MakeToolbarItems()
- {
- ApplyTemplate();
- var toolBar = Template.FindName("ToolBar", this) as ToolStrip;
-
- // put items with QatItem = true in the quick access toolbar
- if (toolBar != null)
- {
- toolBar.Items.Clear();
- for (int i = 0; i < this.Items.Count; i++)
- {
- var item = this.Items[i];
- if (item is ToolStripMenuItem menuItem)
- {
- var swfMenuItem = menuItem.Tag as System.Windows.Forms.ToolStripMenuItem;
- if (swfMenuItem != null && swfMenuItem.QatItem)
- {
- // add visible items to toolbar
- if (swfMenuItem.Visible)
- toolBar.Items.Add(CreateToolbarButton(swfMenuItem));
-
- this.Items.RemoveAt(i);
- i--;
- }
- }
- else if (item is Separator)
- {
- toolBar.Items.Add(new Separator() { Margin = new System.Windows.Thickness(4) });
- }
- }
- RemoveDuplicateSeparators(this.Items);
- RemoveDuplicateSeparators(toolBar.Items);
- TrimSeparators(this.Items);
- TrimSeparators(toolBar.Items);
- if (toolBar.Items.Count > 0)
- VerticalOffset -= 30;
- }
- }
- }
- }
|