我有以下问题:
Shell 中有一个 Ribbon-region,我们称之为“ShellRibbonRegion”。还有一个任务按钮区域“ShellTaskButtonRegion”(类似于使用 Prism 4 创建视图切换应用程序)。
有例如三个模块。每个模块都有不同数量的 RibbonTabItems。模块 1 有一个 RibbonTabItem,模块 2 有四个,模块 3 有一个。
现在的目标是在单击模块的“TaskButton”后将 RibbonTabItems 添加到“ShellRibbonRegion”。我已经编写了一个自定义 RegionAdapter,但问题是要么只显示一个 RibbonTabItem (SingleActiveRegion),要么来自所有模块的所有 (AllActiveRegion) RibbonTabItems。
public class RibbonRegionAdapter : RegionAdapterBase<Ribbon>
{
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="behaviorFactory">Allows the registration of the default set of RegionBehaviors.</param>
public RibbonRegionAdapter(IRegionBehaviorFactory behaviorFactory)
: base(behaviorFactory)
{
}
/// <summary>
/// Adapts a WPF control to serve as a Prism IRegion.
/// </summary>
/// <param name="region">The new region being used.</param>
/// <param name="regionTarget">The WPF control to adapt.</param>
protected override void Adapt(IRegion region, Ribbon regionTarget)
{
regionTarget.SelectedTabChanged += (sender, args) =>
{
if (regionTarget.SelectedTabItem == null)
return;
//region.Activate(regionTarget.SelectedTabItem);
};
region.Views.CollectionChanged += (sender, e) =>
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (UIElement element in e.NewItems)
{
if(element is Ribbon)
this.AddRibbon(element as Ribbon, regionTarget, region);
else if(element is RibbonTabItem)
this.AddRibbonTabItem(element as RibbonTabItem, regionTarget, region);
else if(element is Backstage)
this.AddBackstage(element as Backstage, regionTarget);
}
break;
case NotifyCollectionChangedAction.Remove:
foreach (UIElement elementLoopVariable in e.OldItems)
{
var element = elementLoopVariable;
if (element is Ribbon)
this.RemoveRibbon(element as Ribbon, regionTarget);
else if (element is RibbonTabItem)
this.RemoveRibbonTabItem(element as RibbonTabItem, regionTarget);
else if (element is Backstage)
this.RemoveBackstage(element as Backstage, regionTarget);
}
break;
}
};
}
#region Add
private void AddRibbon(Ribbon ribbon, Ribbon targetRibbon, IRegion region)
{
//Add tabs
foreach (var ribbonTabItem in ribbon.Tabs)
{
this.AddRibbonTabItem(ribbonTabItem, targetRibbon, region);
}
}
private void AddRibbonTabItem(RibbonTabItem ribbonTabItem, Ribbon targetRibbon, IRegion region)
{
if (!targetRibbon.Tabs.Contains(ribbonTabItem))
targetRibbon.Tabs.Add(ribbonTabItem);
}
private void AddBackstage(Backstage backstage, Ribbon targetRibbon)
{
}
#endregion
#region Remove
private void RemoveRibbon(Ribbon ribbon, Ribbon targetRibbon)
{
var tmp = new List<RibbonTabItem>(ribbon.Tabs);
//Add tabs
foreach (var ribbonTabItem in tmp)
{
if (targetRibbon.Tabs.Contains(ribbonTabItem)) this.RemoveRibbonTabItem(ribbonTabItem, targetRibbon);
}
}
private void RemoveRibbonTabItem(RibbonTabItem ribbonTabItem, Ribbon targetRibbon)
{
if (ribbonTabItem is IRegionMemberLifetime)
{
var rml = (IRegionMemberLifetime)ribbonTabItem;
if (!rml.KeepAlive) targetRibbon.Tabs.Remove(ribbonTabItem);
return;
}
targetRibbon.Tabs.Remove(ribbonTabItem);
}
private void RemoveBackstage(Backstage backstage, Ribbon targetRibbon)
{
}
#endregion
protected override IRegion CreateRegion()
{
return new AllActiveRegion();
}
}
所需的行为如下:
单击模块的“任务按钮”:从区域中删除所有不属于该模块的 RibbonTabItems,并添加“单击”模块的选项卡项。
我怎样才能实现这种行为?