1

我正在使用 DevExpress 的 DocumentGroup 和 DocumentGroupAdapter(在他们网站上的 E3339 中描述)用于带有 PRISM 6 的 WPF 应用程序。我正在使用范围区域和 INavigationAware,一切都按预期工作,我可以导航到新文档,并且可以看到漂亮的 INavigationAware 界面完全按照我的意愿处理我的视图模型。唯一的问题是,当第二次导航到(视图的 INavigationAware 确实按预期工作)时,实际的 Document(如选项卡控件中的选项卡)没有被激活(意味着选项卡变得可见)。

4

2 回答 2

1

我根据布莱恩的回答得到了它。对于任何对此感兴趣的人来说,我的解决方案是我首先使用视图在我的视图模型上实现 IPanelInfo。结果是,当再次导航到已经打开的文档面板时,它会被激活,而之前没有。注意目前测试不佳;-)

public class DocumentGroupAdapter : RegionAdapterBase<DocumentGroup>
{
    public DocumentGroupAdapter(IRegionBehaviorFactory behaviorFactory) :
        base(behaviorFactory)
    {
    }

    protected override IRegion CreateRegion()
    {
        return new SingleActiveRegion();
    }
    protected override void Adapt(IRegion region, DocumentGroup regionTarget)
    {
        region.Views.CollectionChanged += (s, e) => {
            OnViewsCollectionChanged(regionTarget, e);
        };
        var manager = regionTarget.GetDockLayoutManager();
        manager.DockItemClosing += (s, e) =>
        {
            Closing(region, e);
        };
        manager.ClosingBehavior = ClosingBehavior.ImmediatelyRemove;
    }

    protected override void AttachBehaviors(IRegion region, DocumentGroup regionTarget)
    {
        base.AttachBehaviors(region, regionTarget);

        if (!region.Behaviors.ContainsKey(DocumentGroupSyncBehavior.BehaviorKey))
            region.Behaviors.Add(DocumentGroupSyncBehavior.BehaviorKey, new DocumentGroupSyncBehavior() { HostControl = regionTarget });
    }


    private static void Closing(IRegion region, ItemEventArgs e)
    {
        var documentPanel = e.Item as DocumentPanel;
        var view = documentPanel?.Content;
        if (view == null) return;
        var v = view as FrameworkElement;
        var info = v?.DataContext as IPanelInfo;
        info?.Close();
        region.Remove(view);
    }

    private static void OnViewsCollectionChanged(DocumentGroup regionTarget, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Add)
            foreach (var view in e.NewItems)
            {
                var manager = regionTarget.GetDockLayoutManager();
                var panel = manager.DockController.AddDocumentPanel(regionTarget);
                panel.Content = view;
                panel.ClosingBehavior = ClosingBehavior.ImmediatelyRemove;
                var v = view as FrameworkElement;
                var info = v?.DataContext as IPanelInfo;
                if (info != null)
                {
                    var myBinding = new Binding
                    {
                        Source = v.DataContext,
                        Path = new PropertyPath("Caption"),
                        Mode = BindingMode.TwoWay,
                        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                    };
                    BindingOperations.SetBinding(panel, BaseLayoutItem.CaptionProperty, myBinding); 
                }
                manager.DockController.Activate(panel);
            }
    }
}

public class DocumentGroupSyncBehavior : RegionBehavior, IHostAwareRegionBehavior
{
    public const string BehaviorKey = "DocumentGroupRegionActiveAwareBehavior";

    private DocumentGroup _hostControl;
    public DependencyObject HostControl
    {
        get { return _hostControl; }
        set { _hostControl = value as DocumentGroup; }
    }

    protected override void OnAttach()
    {
        _hostControl.SelectedItemChanged += HostControl_SelectedItemChanged;
        Region.ActiveViews.CollectionChanged += ActiveViews_CollectionChanged;
    }

    private void HostControl_SelectedItemChanged(object sender, SelectedItemChangedEventArgs e)
    {
        if (e.OldItem != null)
        {
            var item = e.OldItem;
            //are we dealing with a DocumentPanel directly
            if (Region.Views.Contains(item) && Region.ActiveViews.Contains(item))
            {
                Region.Deactivate(item);
            }
            else
            {
                //now check to see if we have any views that were injected
                var contentControl = item as DocumentPanel;
                if (contentControl != null)
                {
                    var injectedView = contentControl.Content;
                    if (Region.Views.Contains(injectedView) && Region.ActiveViews.Contains(injectedView))
                        Region.Deactivate(injectedView);
                }
            }
        }

        if (e.Item != null)
        {
            var item = e.Item;

            //are we dealing with a DocumentPanel directly
            if (Region.Views.Contains(item) && !Region.ActiveViews.Contains(item))
            {
                Region.Activate(item);
            }
            else
            {
                //now check to see if we have any views that were injected
                var contentControl = item as DocumentPanel;
                if (contentControl != null)
                {
                    var injectedView = contentControl.Content;
                    if (Region.Views.Contains(injectedView) && !Region.ActiveViews.Contains(injectedView))
                        Region.Activate(injectedView);
                }
            }
        }
    }

    private void ActiveViews_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            //are we dealing with a view
            var frameworkElement = e.NewItems[0] as FrameworkElement;
            if (frameworkElement != null)
            {
                var documentPanel = GetContentPaneFromView(frameworkElement);
                if (documentPanel != null && !documentPanel.IsActive)
                    documentPanel.ActivateCommand.Execute(null);
            }
            else
            {
                //must be a viewmodel
                var viewModel = e.NewItems[0];
                var contentPane = GetContentPaneFromFromViewModel(viewModel);
                contentPane?.ActivateCommand.Execute(null);
            }
        }
    }

    private DocumentPanel GetContentPaneFromView(object view)
    {
        foreach (var baseLayoutItem in _hostControl.Items)
        {
            var contentPane = (DocumentPanel) baseLayoutItem;
            if (contentPane?.Content != null && contentPane.Content == view)
                return contentPane;
        }
        return null;
    }

    private DocumentPanel GetContentPaneFromFromViewModel(object viewModel)
    {
        foreach (var baseLayoutItem in _hostControl.Items)
        {
            var contentPane = (DocumentPanel) baseLayoutItem;
            var content = contentPane?.Content as FrameworkElement;
            if (content != null && content.DataContext == viewModel)
                return contentPane;
        }
        return null;
    }
}


public interface IPanelInfo
{
    string Caption { get; set; }
    void Close();
}
于 2016-11-30T04:03:33.267 回答
0

我不熟悉 DevExrpess 控件,但我有 Infragistics xamDockManager 控件的自定义区域适配器和 IActiveAware 行为。检查代码,看看您是否可以修改它以与您的控件供应商一起使用。

https://github.com/brianlagunas/xamDockManager-Region-Adapter

于 2016-11-29T22:13:25.827 回答