190 lines
7.6 KiB
C#
190 lines
7.6 KiB
C#
using System;
|
|
using System.Collections.Specialized;
|
|
using System.ComponentModel;
|
|
using System.Configuration;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using DTS.Common.Classes;
|
|
using DTS.Common.Enums;
|
|
using DTS.Common.Events;
|
|
using DTS.Common.Base;
|
|
using Microsoft.Practices.Prism.Events;
|
|
using Microsoft.Practices.Prism.Regions;
|
|
using Microsoft.Windows.Controls.Ribbon;
|
|
|
|
//https://fluent.codeplex.com/SourceControl/latest
|
|
namespace DTS.Common.RibbonControl
|
|
{
|
|
/// <summary>
|
|
/// Enables use of the <see cref="Microsoft.Windows.Controls.Ribbon.Ribbon"/> in a Prism <see cref="RegionNames.RibbonRegion"/>.
|
|
/// To install Ribbon Controls Library, run the following command in the Package Manager Console - Install-Package RibbonControlsLibrary
|
|
/// or install from https://www.microsoft.com/en-us/download/details.aspx?id=11877
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The <see cref="RibbonRegionAdapter"/> adapts controls derived from the class <see cref="Microsoft.Windows.Controls.Ribbon.Ribbon"/> such as <see cref="RibbonTab"/>.
|
|
/// </remarks>
|
|
public class RibbonRegionAdapter : RegionAdapterBase<Ribbon>, IDisposable
|
|
{
|
|
/// <summary>
|
|
/// Injected region manager
|
|
/// </summary>
|
|
private readonly IRegionManager _regionManager;
|
|
|
|
/// <summary>
|
|
/// Injected event aggregator
|
|
/// </summary>
|
|
private readonly IEventAggregator _eventAggregator;
|
|
|
|
private static readonly object Lock = new object();
|
|
|
|
/// <summary>
|
|
/// Creates a new instance of the <see cref="RibbonRegionAdapter"/> class.
|
|
/// </summary>
|
|
/// <param name="regionBehaviorFactory">Allows the registration of the default set of RegionBehaviors.</param>
|
|
/// <param name="regionManager">Obtained reference of the region manager by using dependency injection.</param>
|
|
/// <param name="eventAggregator">Obtained reference of the revent aggregator by using dependency injection.</param>
|
|
public RibbonRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory, IRegionManager regionManager, IEventAggregator eventAggregator)
|
|
: base(regionBehaviorFactory)
|
|
{
|
|
_regionManager = regionManager;
|
|
_eventAggregator = eventAggregator;
|
|
_eventAggregator.GetEvent<TabControlSelectionChanged>().Subscribe(OnTabControlSelectionChanged);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adapts the <see cref="Microsoft.Windows.Controls.Ribbon.Ribbon"/> to an <see cref="IRegion"/>.
|
|
/// </summary>
|
|
/// <param name="region">The new region being used.</param>
|
|
/// <param name="regionTarget">The object to adapt.</param>
|
|
protected override void Adapt(IRegion region, Ribbon regionTarget)
|
|
{
|
|
if (regionTarget == null)
|
|
throw new ArgumentNullException("regionTarget");
|
|
|
|
region.ActiveViews.CollectionChanged += (o, e) =>
|
|
{
|
|
switch (e.Action)
|
|
{
|
|
case NotifyCollectionChangedAction.Add:
|
|
foreach (var item in e.NewItems.OfType<RibbonTab>())
|
|
AddRibbonTabToRegion(item, regionTarget);
|
|
foreach (var item in e.NewItems.OfType<RibbonApplicationMenu>())
|
|
AddApplicationMenuToRegion(item, regionTarget);
|
|
|
|
break;
|
|
case NotifyCollectionChangedAction.Remove:
|
|
foreach (var item in e.OldItems.OfType<RibbonTab>())
|
|
RemoveRibbonTabFromRibbonRegion(item, regionTarget);
|
|
|
|
break;
|
|
}
|
|
};
|
|
}
|
|
|
|
private void AddRibbonTabToRegion(RibbonTab ribbonTab, Ribbon regionTarget)
|
|
{
|
|
lock (Lock)
|
|
{
|
|
if (string.IsNullOrEmpty(ribbonTab.Uid))
|
|
ribbonTab.Uid = Guid.NewGuid().GetHashCode().ToString(CultureInfo.InvariantCulture);
|
|
|
|
regionTarget.Items.Add(ribbonTab);
|
|
}
|
|
|
|
regionTarget.Items.SortDescriptions.Add(new SortDescription("TabIndex", ListSortDirection.Ascending));
|
|
|
|
var config = ConfigurationManager.AppSettings["DefaultRibbonTab"];
|
|
if (!string.IsNullOrWhiteSpace(config))
|
|
{
|
|
ribbonTab.IsSelected = ribbonTab.Header.ToString().ToLower() == config.ToLower();
|
|
}
|
|
}
|
|
|
|
private void AddApplicationMenuToRegion(RibbonApplicationMenu applicationMenu, Ribbon regionTarget)
|
|
{
|
|
if (string.IsNullOrEmpty(applicationMenu.Uid))
|
|
applicationMenu.Uid = Guid.NewGuid().GetHashCode().ToString(CultureInfo.InvariantCulture);
|
|
|
|
regionTarget.ApplicationMenu = applicationMenu;
|
|
}
|
|
|
|
private void RemoveRibbonTabFromRibbonRegion(RibbonTab ribbonTab, Ribbon regionTarget)
|
|
{
|
|
regionTarget.Items.Remove(ribbonTab);
|
|
}
|
|
|
|
private void OnTabControlSelectionChanged(TabControlSelectionEventArgs e)
|
|
{
|
|
if (e.Operation == TabControlOperation.RemovedItem) return;
|
|
|
|
var view = e.Item as IBaseView;
|
|
if (view == null) return;
|
|
if (view.DataContext == null) return;
|
|
|
|
var ribbonTabInfo = view.DataContext as IRibbonTabInfoProvider;
|
|
if (ribbonTabInfo == null) return;
|
|
if (string.IsNullOrEmpty(ribbonTabInfo.RibbonTabUid)) return;
|
|
|
|
var currentTab = _regionManager.Regions[RegionNames.RibbonRegion].ActiveViews.OfType<RibbonTab>().FirstOrDefault(t => t.IsSelected);
|
|
if (currentTab == null) return;
|
|
|
|
if (ribbonTabInfo.RibbonTabUid != currentTab.Uid)
|
|
{
|
|
var selectedTab = _regionManager.Regions[RegionNames.RibbonRegion].ActiveViews.OfType<RibbonTab>().FirstOrDefault(t => t.Uid == ribbonTabInfo.RibbonTabUid);
|
|
|
|
if (selectedTab != null)
|
|
selectedTab.IsSelected = true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Template method to create a new instance of <see cref="Microsoft.Practices.Prism.Regions.IRegion"/>
|
|
/// that will be used to adapt the object.
|
|
/// </summary>
|
|
/// <returns>
|
|
/// A new instance of <see cref="Microsoft.Practices.Prism.Regions.IRegion"/>.
|
|
/// </returns>
|
|
protected override IRegion CreateRegion()
|
|
{
|
|
return new AllActiveRegion();
|
|
}
|
|
|
|
#region IDisposable Support
|
|
|
|
private bool _disposedValue; // To detect redundant calls.
|
|
|
|
/// <summary>
|
|
/// Disposes the object.
|
|
/// </summary>
|
|
/// <param name="disposing">True if called by the public Dispose method.</param>
|
|
protected virtual void Dispose(bool disposing)
|
|
{
|
|
if (!_disposedValue)
|
|
{
|
|
if (_eventAggregator != null)
|
|
_eventAggregator.GetEvent<TabControlSelectionChanged>().Unsubscribe(OnTabControlSelectionChanged);
|
|
}
|
|
_disposedValue = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Disposes the object.
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
|
|
Dispose(true);
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Object finalizer.
|
|
/// </summary>
|
|
~RibbonRegionAdapter()
|
|
{
|
|
Dispose(false);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |