如何在不选择视图的情况下将视图添加到PRISM TabControl区域? [英] How to add view to a PRISM TabControl region WITHOUT making it selected?

查看:324
本文介绍了如何在不选择视图的情况下将视图添加到PRISM TabControl区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个使用PRISM的WPF应用程序,其区域类型为TabControl。

 < TabControl pyramid:RegionManager.RegionName = {x:静态inf:RegionNames.ContentRegion}> 
< TabControl.ItemContainerStyle>
< Style TargetType = {x:Type TabItem}>
< Setter Property = Header Value = {Binding TabName} />
< / Style>
< /TabControl.ItemContainerStyle>
< / TabControl>

我们正在用



<$ p注册视图$ p> _regionManager.RegisterViewWithRegion(RegionNames.ContentRegion,typeof(ContentView));

问题是,这样可以自动选择已注册的选项卡。有没有一种方法可以将视图添加为选项卡但不能选择它?

解决方案

我想出的解决方案是实现



注意:此接口还允许您指定选项卡顺序(如果有)

 公共接口ITabItemView 
{
int TabItemIndex {get; }

bool IsStartupTab {get; }
}

公共类TabControlRegionAdapter:RegionAdapterBase< TabControl>
{
private ITabItemView startupTab = null;

public TabControlRegionAdapter(IRegionBehaviorFactory factory)
:base(factory)
{

}

保护重写void Adapt( IRegion区域,TabControl regionTarget)
{
region.Views.CollectionChanged + =(s,e)=>
{
if(e.Action == NotifyCollectionChangedAction.Add)
{
var items = regionTarget.Items;

foreach(e.NewItems中的ITabItemView选项卡)
{
if(tab.TabItemIndex> items.Count)
items.Add(tab);
else
个项目。Insert(tab.TabItemIndex,tab);

if(tab.IsStartupTab)
{
if(tab!= startupTab&& startupTab!= null)
throw new InvalidOperationException(多个tab是启动标签。);

startupTab =选项卡;

regionTarget.SelectedItem =选项卡;
}
}
}
};
}

受保护的覆盖IRegion CreateRegion()
{
return new AllActiveRegion();
}
}

当然,在您的Bootstrapper类中,您需要

 受保护的重写Microsoft.Practices.Prism.Regions.RegionAdapterMappings ConfigureRegionAdapterMappings()
{
var mappings = base。 ConfigureRegionAdapterMappings();
mappings.RegisterMapping(typeof(TabControl),Container.Resolve< TabControlRegionAdapter>());
返回映射;
}


We have a WPF application using PRISM, with a region of type TabControl.

    <TabControl prism:RegionManager.RegionName="{x:Static inf:RegionNames.ContentRegion}">
        <TabControl.ItemContainerStyle>
            <Style TargetType="{x:Type TabItem}">
                <Setter Property="Header" Value="{Binding TabName}" />
            </Style>
        </TabControl.ItemContainerStyle>
    </TabControl>

And we are registering views with

_regionManager.RegisterViewWithRegion(RegionNames.ContentRegion, typeof(ContentView));

Problem is, this way the registered tab automatically gets selected. Is there a way to add a view as tab but NOT select it??

解决方案

The solution I came up with is to implement an interface for each view, and implement a custom RegionAdapter which uses it.

Note: This interface also allows you to specify tab order, if you need it as well.

public interface ITabItemView
{
    int TabItemIndex { get; }

    bool IsStartupTab { get; }
}

public class TabControlRegionAdapter : RegionAdapterBase<TabControl>
{
    private ITabItemView startupTab = null;

    public TabControlRegionAdapter(IRegionBehaviorFactory factory)
        : base(factory)
    {

    }

    protected override void Adapt(IRegion region, TabControl regionTarget)
    {
        region.Views.CollectionChanged += (s, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    var items = regionTarget.Items;

                    foreach (ITabItemView tab in e.NewItems)
                    {
                        if (tab.TabItemIndex > items.Count)
                            items.Add(tab);
                        else
                            items.Insert(tab.TabItemIndex, tab);

                        if (tab.IsStartupTab)
                        {
                            if (tab != startupTab && startupTab != null)
                                throw new InvalidOperationException("More than one tab is the startup tab.");

                            startupTab = tab;

                            regionTarget.SelectedItem = tab;
                        }
                    }
                }
            };
    }

    protected override IRegion CreateRegion()
    {
        return new AllActiveRegion();
    }
}

And of course in your Bootstrapper class you need

protected override Microsoft.Practices.Prism.Regions.RegionAdapterMappings ConfigureRegionAdapterMappings()
    {
        var mappings = base.ConfigureRegionAdapterMappings();
        mappings.RegisterMapping(typeof(TabControl), Container.Resolve<TabControlRegionAdapter>());
        return mappings;
    }

这篇关于如何在不选择视图的情况下将视图添加到PRISM TabControl区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆