用棱镜导航时,在文档组区域中激活devexpress documentpanel



我正在使用DeVexpress的DocumentGroup和DocumentGroupAdapter(在其网站上的E3339中进行了描述),用于带有Prism 6的WPF应用程序。我正在使用Scoped Regions和InavigationWare,并且一切都按预期工作,我可以导航,我可以导航对于新文档,我可以看到美丽的InavigationAware界面,完全按照我的意愿在视图模型上工作。唯一的问题是,当导航到第二次导航时,实际文档(例如选项卡控件中的一个选项卡)不会被激活(这意味着该选项卡变得可见)(视图的IntavigationAware确实可以按预期工作)。

我根据布莱恩的答案使它起作用。对于我感兴趣的任何人,我的解决方案是我首先使用视图在视图模式上实现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();
}

我不熟悉DEVEXRPESS控件,但是我有一个自定义的区域适配器和IACTIVEAWARE行为,用于Infragistics XamdockManager Control。查看代码,看看是否可以将其修改以与控制供应商一起使用。

https://github.com/brianlagunas/xamdockmanager-region-apapter

最新更新