0

我有以下问题:

Shell 中有一个 Ribbon-region,我们称之为“ShellRibbonRegion”。还有一个任务按钮区域“ShellTask​​ButtonRegion”(类似于使用 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,并添加“单击”模块的选项卡项。

我怎样才能实现这种行为?

4

1 回答 1

0

我遇到了类似的问题,并最终跟踪添加到由模块类型名称键入的哈希表中的选项卡。模块选项卡是在包含功能区控件的模块视图中开发的,因此还需要传输数据上下文。这是我最终得到的 RibbonRegionAdapter(添加您自己的命名空间!):

// Based on
// http://www.codeproject.com/Articles/165370/Creating-View-Switching-Applications-with-Prism-4#AppendixA
// with my modifications.

/// <summary>
/// Enables use of a Ribbon control as a Prism region.
/// </summary>
/// <remarks> See Developer's Guide to Microsoft Prism (Ver. 4), p. 189.</remarks>
[Export]
public class RibbonRegionAdapter : RegionAdapterBase<Ribbon> {

    private static readonly Hashtable RibbonTabs = new Hashtable();

    /// <summary>
    /// Default constructor.
    /// </summary>
    /// <param name="behaviorFactory">Allows the registration
    /// of the default set of RegionBehaviors.</param>
    [ImportingConstructor] 
    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) {
        region.Views.CollectionChanged += (sender, e) => {
            switch (e.Action) {
                case NotifyCollectionChangedAction.Add:
                    foreach (FrameworkElement element in e.NewItems) {
                        if (element is Ribbon) {

                            Ribbon rb = element as Ribbon;
                            var tabList = new List<RibbonTab>();

                            var items = rb.Items;

                            for (int i = rb.Items.Count - 1; i >= 0; i--) {
                                if (!(rb.Items[i] is RibbonTab)) continue;
                                RibbonTab rt = (rb.Items[i] as RibbonTab);
                                rb.Items.Remove(rt); // remove from existing view ribbon
                                regionTarget.Items.Add(rt); // add to target region ribbon
                                tabList.Add(rt); // add to tracking list

                                // Without these next 3 lines the tabs datacontext would end up being inherited from the Ribbon to which 
                                // it has been transferred.
                                // Not sure if this is the best place to do this but it works for my purposes at the moment
                                if (rt.DataContext.Equals(regionTarget.DataContext)) { // then it is inherited
                                    rt.DataContext = rb.DataContext; // so set it explicitly to the original parent ribbons datacontext
                                }

                            }
                            // store tracking list in hashtable using string key (= the view type name)
                            var key = rb.GetType().Name;
                            RibbonTabs[key] = tabList;

                        } else if (element is RibbonTab) {
                            // the datacontext should already be set in  these circumstances
                            regionTarget.Items.Add(element);
                        }
                    }
                    break;

                case NotifyCollectionChangedAction.Remove:
                    foreach (UIElement elementLoopVariable in e.OldItems) {

                        var element = elementLoopVariable;

                        if (element is Ribbon) {

                            Ribbon rb = element as Ribbon;
                            var key = rb.GetType().Name;
                            if (!RibbonTabs.ContainsKey(key)) continue; // no ribbon tabs have been tracked

                            var tabList = (RibbonTabs[key] as List<RibbonTab>) ?? new List<RibbonTab>();
                            foreach (RibbonTab rt in tabList)
                            {
                                if (!regionTarget.Items.Contains(rt)) continue; // this shouldn't happen
                                regionTarget.Items.Remove(rt); // remove from  target region ribbon
                                rb.Items.Add(rt); // restore to view ribbon
                            }
                            RibbonTabs.Remove(key); // finished tracking so remove from hashtable

                        } else if (regionTarget.Items.Contains(element)) {
                            regionTarget.Items.Remove(element);
                        }
                    }
                    break;
            }
        };
    }

    protected override IRegion CreateRegion() {
        return new SingleActiveRegion();
    }
}
于 2014-05-21T15:30:40.330 回答