AvalonDock Pane RegionAdapter

Topics: Prism v4 - WPF 4
Jun 5, 2011 at 9:51 AM

Hi,

I am using AvalonDock with prism.

I have written a simple PaneRegionAdapter from a example i found on the net (below)

now this was working fine in the following example


var dcViewModel = ServiceLocator.Current.GetInstance<DocumentContentViewModel>();
var cdView = new DocumentContentView() {DataContext = dcViewModel};
_regionManager.Regions["regionName"].Add(cdView)

but i wanted to do it in a way where i don't have to set the DataContext all the time.

so the way i am trying to do it now is the following

 
 // ResourceDictionary.xaml
 <DataTemplate DataType="{x:Type ViewModels:DocumentContentViewModel}" >
 	<Views:DocumentContentView/>
 </DataTemplate>

 
// add in the resource when module loads (this seem to load correctly
ResourceDictionary rd = new ResourceDictionary();
rd.Source = new Uri("pack://application:,,,/<moduleName>;Component/Views/ResourceDictionary.xaml");
Application.Current.Resources.MergedDictionaries.Add(rd);

// add the viewModel to the region 
var dcViewModel = ServiceLocator.Current.GetInstance<DocumentContentViewModel>();
_regionManager.Regions["regionName"].Add(dcViewModel);


now the adapters OnViewsCollectionChanged event gets called but the NotifyCollectionChangedEventArgs.NewItem 
is of type DocumnetContentViewModel which means that nothing is getting added to the regionTarget. 

What i would be expecting is that prism would have resolved DocumentContentViewModel to a DocumentContentView through the resourceDictonary.

I was looking at other examples that do it this way but not to a AvalonDock. I think it's because i'm not adding any Behaviors to the adapter 
but i wouldn't know where to start with that as i have (re)read the Prism4 pdf.... with no luck 
I was wondering:
1) if anyone could give me some hints on how this could/should be done? 
2) do i need to resolve the mapping manually? or is Prism meant to do this? 
3) am i barking up the wrong tree?
 
Thank you

Rhys
public class PaneRegionAdapter : RegionAdapterBase<Pane>
    {
        public delegate void DockableContentHandler();
 
        public PaneRegionAdapter( IRegionBehaviorFactory regionBehaviorFactory ) : base( regionBehaviorFactory )
        {
        }
 
        protected override IRegion CreateRegion()
        {
            return new Region();
        }
 
        protected override void Adapt(IRegion region, Pane regionTarget)
        {
            region.Views.CollectionChanged += delegate(object sender, NotifyCollectionChangedEventArgs e)
            {
                OnViewsCollectionChanged(sender, e, region, regionTarget);
            };
        }
 
     
        private void OnViewsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e, IRegion region, Pane regionTarget)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    foreach (object item in e.NewItems)
                    {
                        var newContent = item as ManagedContent;
 
                        if (newContent != null)
                        {
                            regionTarget.Items.Add(newContent);
                            newContent.InvalidateParents();
                        }
                    }
                    break;
            }
        }
    }





 

Developer
Jun 6, 2011 at 6:52 PM

Hi,

You might find the articles mentioned in this thread useful to your purpose.

If you can't find a solution to your problem by reading the aformentioned articles, it would be helpful if you could provide us with a repro sample application, so that we can help you further with this.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Aug 17, 2011 at 10:41 AM

Hi!

I have looked around to find a complete example of how to use AvalonDock for handling regions in Prism 4.0. Since I'm pretty new to both these framworks I would love to have a complete sample application. There seems to a bunch of code snippets out there but no one is complete as far as I can see.

Peter Svahn

Developer
Aug 17, 2011 at 8:09 PM

Hi,
I think you might find this link helpful, there you will find an application which merged Prism and AvalonDock for the GUI:

Also note that  regardless of which Region Adapter you are using, the behavior of a region remains the same. You'll fin more information regarding how the Regions mechanism works in prism in the Composing the User Interface chapter:


I hope you find this useful.

Agustin Adami
http://blogs.southworks.net/aadami

Aug 18, 2011 at 7:04 AM

Thank you for your reply!

I have seen the Sofa library too and It is probably great, but I would like to avoid bringing in yet another library. I was more looking for a "standard" example of how to implement a region adapter for AvalonDock in Prism, and I have found a couple, like Prism and WPF. Custom Tab region adapter. However they are not complete and have some loose ends. So if someone have the missing pices, please let me know.

Peter Svahn

Oct 11, 2012 at 6:58 PM

Check the implemantion of PRISM  that uses MEF + Avalon Dock + elysium + WPF ... http://central.codeplex.com/