支持MVVM的WPF对接库

时间:2011-09-20 13:05:08

标签: wpf mvvm dock

我已经看过像this one这样的问题了,但是我想使用的停靠库必须有一个重要的功能,没有被问到:它必须支持MVVM。

那么,在Telerik,DotNetBar,DevZest和其他库(不包括我已经测试过的AvalonDock)中,有没有你真正使用MVVM?

提前致谢

1 个答案:

答案 0 :(得分:2)

你好迈克试试这个:

  1. 简单方法:实施Sofa, An adaptation of AvalonDock for Prism
  2. 使用AvalonDock并实现这样的自定义区域适配器:

    public class ResizingPanelRegionAdapter : RegionAdapterBase<DockingManager>
    {
    public ResizingPanelRegionAdapter(IRegionBehaviorFactory factory)
        : base(factory)
    {
    
    }
    protected override IRegion CreateRegion()
    {
        return new AllActiveRegion();
    }
    
    protected override void Adapt(IRegion region, DockingManager regionTarget)
    {
        region.Views.CollectionChanged += delegate(Object sender, NotifyCollectionChangedEventArgs e)
        {
            OnViewsCollectionChanged(sender, e, region, regionTarget);
        };
    }
    
    private void OnViewsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e, IRegion region, DockingManager regionTarget)
    {
        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            foreach (object item in e.NewItems)
            {
                UIElement view = item as UIElement;
                if (view != null)
                {
                    //Get 
                    ResizingPanel resizingPanel = GetResizingPanel(regionTarget.Content);
                    resizingPanel.Background = Brushes.White;
                    DocumentPane document = GetDocumentPane(resizingPanel.Children);
                    //document.Background = Brushes.White;
    
                    DocumentContent newContentPane = new DocumentContent();
                    newContentPane.Content = item;
                    var itemView = (item as IViewBase);
                    if (itemView != null)
                        newContentPane.Title = itemView.Title;
                    //When contentPane is closed remove the associated region
                    newContentPane.Closed += (contentPaneSender, args) =>
                    {
                        region.Remove(item);
                        newContentPane.Content = null;
                    };
    
                    document.Items.Add(newContentPane);
    
                    if (!resizingPanel.Children.Contains(document))
                        resizingPanel.Children.Add(document);
    
                    regionTarget.Content = resizingPanel;
    
                    newContentPane.Activate();
                    region.Activate(item);
                }
            }
        }
        else if (e.Action == NotifyCollectionChangedAction.Remove)
        {
    
        }
    }
    
    private DocumentPane GetDocumentPane(UIElementCollection collection)
    {
        foreach (object item in collection)
        {
            var documentPanel = item as DocumentPane;
            if (documentPanel != null)
                return documentPanel;
        }
        return new DocumentPane();
    }
    
    private ResizingPanel GetResizingPanel(object content)
    {
        var resizingPanel = content as ResizingPanel;
        if (resizingPanel != null)
            return resizingPanel;
        return new ResizingPanel();
    }    
    }
    
  3. 你在XAML中可以像这样实现它:

    <avalon:DockingManager prism:RegionManager.RegionName="MainRegion">
    </avalon:DockingManager>
    

    它是如何工作的? 很简单,首先你必须记住Region adapters are responsible for creating a region and associating it with the control. This allows you to use the IRegion interface to manage the UI control contents in a consistent way.

    DockingManager is the core control in AvalonDock. It arranges contained panes, handles fly out panes and floating windows.

    所以,按照这个例子你可以为avalon实现一个自定义区域适配器,我在一个项目中使用了这个实现,获得了很棒的结果。

    此致