I'm trying to create a reusable toolbar as UserControl, that also supports saving the layout. Some of my view might need to add a few more buttons, so I'm using BarAction to add, remove and update the standard toolbar. Since I'm saving and restoring the layout, I need to use the BarManager.
Now here's the issue: inserting new BarButtonItems into the toolbar, does not add them to the customization window tab "Commands". Run the attachted sample to see, that "MyNewlyAddedButton" does not appear in the customization window.
If I were to just use a ToolBarControl in my UserControl, this would work fine, but then I couldn't save and restore my layout.
So my question is how do I add the inserted BarButtonItems to the custimazation window? Or alternatively how do I save the layout of a ToolBarControl?
Either one would solve my problem.
Thanks in advance.
(I omitted the saving and restoring part from the sample.)
The cause of this behavior is that this inserted BarItem isn't initially defined in the same BarManager. To change this behavior and display it, it's necessary to create an AllItemsBarManagerCategory class descendant and override its GetBarItems method in the following manner to allow search for bar items in the BarManager scope's ancestors:
Public Class BarManagerCategoryEx Inherits AllItemsBarManagerCategory Public Sub New(skipNameCheck As Boolean, name As String, scopeNode As FrameworkElement, isSpecialCategory As Boolean, caption As String) Me.SkipNameCheck = skipNameCheck Me.Name = name Me.ScopeNode = scopeNode Me.IsSpecialCategory = isSpecialCategory Me.Caption = caption End Sub Public Property SkipNameCheck() As Boolean Public Overrides Function GetBarItems() As List(Of BarItem) Return _ BarNameScope.GetService (Of IElementRegistratorService)(ScopeNode).GetElements (Of BarItem)( ScopeSearchSettings.Local Or ScopeSearchSettings.Ancestors).Where( function(item) SkipNameCheck Or Name.Equals(item.CategoryName)).ToList() End Function End Class
After that, use this class descendant to override BarManager's default categories:
Public Class BarManagerEx Inherits BarManager Protected Overrides Sub OnLoaded(sender As Object, e As EventArgs) MyBase.OnLoaded(sender, e) Dim categories = Me.Categories.Wrap(Of IBarManagerCategoryCollectionWrapper)() categories.allItems = New BarManagerCategoryEx(True, "All Items", Me, true, BarsLocalizer.GetString(BarsStringId.BarManagerCategory_AllItemsCategoryCaption)) categories.unassignedItems = New BarManagerCategoryEx(False, "Unassigned Items", Me, true, BarsLocalizer.GetString(BarsStringId.BarManagerCategory_UnassignedCategoryCaption)) End Sub End Class
I've modified the sample project to demonstrate the main idea of this approach.