ToolBarPanelRegionAdapter

Nov 29, 2008 at 12:46 PM
I want to implement tool bars in my project and ran across this adapter class.

How is this used?  I would think perhaps adapter for ToolBarTray would be useful too.  Has anyone used ToolBarTray as a region?

Willem Meints, I think you added this adapter to this project, would you shed some light please?

Thanks,
Sean
Dec 29, 2008 at 4:00 PM
Sean,

we've used the panel adaptor - declared in the shell as below

        <!--Tool Bars-->
        <ToolBarTray Margin="0,0,0,0" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4" Style="{DynamicResource MainToolBarTray}">
            <ToolBar>
                <ToolBarPanel Orientation="Horizontal" x:Name="MainToolbar" cal:RegionManager.RegionName="{x:Static inf:RegionNames.MainToolbarRegion}"/>
            </ToolBar>
        </ToolBarTray>

then adding controls to the panel (Configuration toolbar is a horizontal stackpanel with some buttons in):

            IAppListViewPresentationModel appListpresentationModel = _container.Resolve<IAppListViewPresentationModel>();
            IRegion mainToolbarRegion = _regionManager.Regions[RegionNames.MainToolbarRegion];
            mainToolbarRegion.Add(appListpresentationModel.View);
            mainToolbarRegion.Activate(appListpresentationModel.View);

            // add config toolbar
            mainToolbarRegion.Add(_container.Resolve<ConfigurationToolbar>());

The only problem I have with this at the moment is that seperators between buttons in my stackpanel don't have the right style.. I may post a question about that on here..

hope it helps

Trev
Jan 20, 2009 at 8:41 AM
Sean,

I agree that a RegionAdapter for the ToolbarTray would be very useful! I created a UIExtensionService which does provide use (beside some other stuff) with the ability to add whole ToolBars to a predefined ToolBarTray.

For adding individual Buttons to the MainToolBar (similar to the example given by Trev) I just define a Region inside a ToolBar without using the ToolBarPanel... so I'm not really sure what usage the ToolBarPanelRegionAdapter has.

Here is the XAML code:

<ToolBarTray Name="ToolBarTray" DockPanel.Dock="Top" IsLocked="False">
            <ToolBar cal:RegionManager.RegionName="{x:Static UI:RegionNames.MainToolBarRegion}" Visibility="{Binding RelativeSource={RelativeSource Self}, Path=Items.Count, Converter={StaticResource ToolBarVisibilityConverter}}" />
</ToolBarTray>

To automatically make the MainToolBar invisible (Collapsed) I bind the Visibility property to the Items.Count Property of the ToolBar using a converter...

Saw similar styling behaviour (styles not beeing applied to buttons) when wrapping some Controls inside a Grid-Panel before adding it to the ToolBar. Any ideas on this?

Chris
Oct 12, 2009 at 3:02 PM
Edited Oct 12, 2009 at 4:02 PM

Hi guys!

I've created a ToolBarTrayRegionAdapter, and it works successfully (it based on this article Prism - Creating a Custom Region Adapter for Ribbon by Prajeesh):

The Solution:

ToolBarTrayRegionAdapter.cs

public class ToolBarTrayRegionAdapter : RegionAdapterBase<ToolBarTray>
    {
        public ToolBarTrayRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
            : base(regionBehaviorFactory)
        { }
        private ToolBarTray _Instance;
        protected override void Adapt(IRegion region, ToolBarTray toolBarTray)
        {
            _Instance = toolBarTray;
            region.ActiveViews.CollectionChanged += new NotifyCollectionChangedEventHandler(
                (sender, e) => 
                {
                    switch (e.Action)
                    {
                        case NotifyCollectionChangedAction.Add:
                            foreach (ToolBar toolBar in e.NewItems)
                            {
                                _Instance.ToolBars.Add(toolBar);
                            }
                            break;

                        case NotifyCollectionChangedAction.Remove:
                            foreach (ToolBar toolBar in e.NewItems)
                            {
                                _Instance.ToolBars.Remove(toolBar);
                            }
                            break;
                    }
                    //region.ActiveViews.ToList().ForEach(t => toolBarTray.ToolBars.Add(t as ToolBar)); throws an exception, but it works without this line 
                }
            );
            
        }

        protected override IRegion CreateRegion()
        {
            return new AllActiveRegion();
        }
    }
Bootstrapper.cs
protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
        {
            RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
            mappings.RegisterMapping(typeof(ToolBarTray), this.Container.Resolve<ToolBarTrayRegionAdapter>());
            return mappings;
        }
ToolBarTrayRegion.xaml
<ToolBarTray Name="ToolBarPanelRegion" cal:RegionManager.RegionName="{x:Static infrastructure:RegionNames.ToolBarPanelRegion}"/>
SearchToolBarView.xaml
<ToolBar x:Class="UI.Modules.Views.ToolBarView.SearchToolBar"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Button Content="Search"></Button>
</ToolBar>
SearchToolBarView.xaml.cs
public partial class SearchToolBar : ToolBar
    {
        public SearchToolBar()
        {
            InitializeComponent();
        }
    }
Try it :)
 
surexxx
Jan 14, 2011 at 9:51 PM

Thanks for your post (from over one year ago).

I discovered one defect. In the adapt method, for case remove, the code should iterate over e.OldItems rather than e.NewItems.