1

PRISM 6 を使用した WPF アプリケーションに DevExpress の DocumentGroup と DocumentGroupAdapter (サイトの E3339 で説明) を使用しています。スコープ領域と INavigationAware を使用しており、すべてが期待どおりに機能し、新しいドキュメントに移動でき、美しい INavigationAware インターフェイスを確認できます。まさに私が望むように、ビューモデルに取り組んでいます。唯一の問題は、実際のドキュメント (タブ コントロール内のタブなど) が 2 回目に移動したときにアクティブにならない (つまり、タブが表示される) ことです (ビューの INavigationAware は期待どおりに機能します)。

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 に答える