I have a WPF application with a ToolBarTray composed of a few ToolBar with tons of buttons. The only thing I want is for them to always be visible, no matter the size of the window.
<ToolBarTray>
<ToolBar OverflowMode="Never">
<userControls:SearchUserControl x:Name="SearchControl" />
</ToolBar>
<ToolBar ItemsSource="{Binding CommonCommands}" OverflowMode="Never"/>
<ToolBar ItemsSource="{Binding Type1Commands}" OverflowMode="Never"/>
<ToolBar ItemsSource="{Binding Type2Commands}" OverflowMode="Never" />
<ToolBar ItemsSource="{Binding Type3Commands}" OverflowMode="Never" />
</ToolBarTray>
I set OverflowMode="Never" to get rid of the little arrow on the right where buttons goes when there's not enough space since I always want all buttons visible. My "commands" list are RoutedCommand with a DataTemplate to show up as a button, but it react the same whatever I use.
If I put the ToolBarTray inside a StackPanel, their buttons/toolbars just continue past the window size. If I put the ToolBarTray inside a WrapPanel, instead of wrapping it hide the buttons completely (only the toolbar grip remains).
The comportment I would love to achieve is for toolbars to change their Band property dynamically so that if there's no space available, the toolbar switch to the second band (row) instead of hiding the buttons in their overflow panel.
Wrapping a ToolBar's content is not easy, but of course it is possible. The point is that the standard
ToolBarcontrol wants aTemplatePartcalled "PART_ToolBarPanel", whose type must beToolBarPanel. The ToolBarPanel inherits fromStackPanel, but for achieving your target, you should use aWrapPanel. So since the ToolBarPanel inherits from StackPanel, it cannot inherit at the same time from WrapPanel.My idea is to extend the ToolBarPanel and to disguise it so it looks like a WrapPanel. First of all let's see the code of our ToolBarPanel (I used ILSpy in order to retrive the WrapPanel code behavior):
Just for the purpose of this sample, I used two static variables (
_itemWidthand_itemHeight) to define the WrapPanel item size. Anyway they should be replaced with two dependecy properties. Moreover the extended ToolBar does not handle the Orientation changes (just use ILSpy to adapt it).In order to make this code work, I added the
DoubleUtilclass too:Now we need just to define a couple of resources in the XAML (ILSpy again):
Now let's create a small Window (300x300) and add this XAML:
I hope my code can help you.