使用PRISM导航时,在DocumentGroup区域中激活DevExpress DocumentPanel

时间:2016-11-29 21:54:04

标签: wpf devexpress prism prism-6

我正在使用DevExpress的DocumentGroup和DocumentGroupAdapter(在他们的网站上用E3339描述)用于PRISM 6的WPF应用程序。我使用范围区域和INAVigationAware,一切都按预期工作,我可以导航到新文档,我可以看到美丽的INavigationAware界面正在我的视图模型上工作,正如我想要的那样。唯一的问题是当导航到第二次时,实际文档(如选项卡控件中的选项卡)不会被激活(意味着选项卡变得可见)(视图的INavigationAware确实按预期工作)。

2 个答案:

答案 0 :(得分:1)

我根据Brian的回答得到了它。对于任何感兴趣的人都是我的解决方案,我首先使用视图在我的viewmodel上实现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();
}

答案 1 :(得分:0)

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

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