HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

MVVM, WPF Ribbon V4, with Prism

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withwpfribbonmvvmprism

Problem

This weekend I've been having one heck of a time getting WPF Ribbon v4 working with MVVM and Prism (using unity). After much trial and error, I believe I have it working. I was hoping someone could take a look at it and give me some feedback.

RibbonRegionAdapter.cs

public class RibbonRegionAdapter : RegionAdapterBase
{
    private Ribbon _ribbonTarget;

    public RibbonRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
        : base(regionBehaviorFactory)
    {

    }

    protected override void Adapt(IRegion region, Ribbon regionTarget)
    {
        _ribbonTarget = regionTarget;

        region.Views.CollectionChanged += delegate {  
            foreach (RibbonTab tab in region.Views.Cast())  
            {  
                if (!_ribbonTarget.Items.Contains(tab))  
                {  
                    _ribbonTarget.Items.Add(tab);  
                }  
            }  
        };  
    }

    protected override IRegion CreateRegion()
    {
        return new SingleActiveRegion();
    }
}


BootStrapper.cs - To register our regionAdapter

protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
    {
        RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();

        if (mappings != null)
        {
            mappings.RegisterMapping(typeof(Ribbon), this.Container.Resolve());
        }

        return mappings;
    }


CarRibbonTab.xaml


    

    
    
        
        
        
    


CarRibbonTab.cs - Code behind for the View

public partial class CarRibbonTab: RibbonTab
{
    public CarRibbonTab()
    {
        InitializeComponent();
    }
}


Shell.xaml - Just showing the ribbon control


    
        
            
        
    


CarModule.cs - registering the view with the region

```
public class CarModule: IModule
{
private readonly IRegionManager _regionManager;
private readonly IUnityContainer _container;
public MenuItemViewModel Me

Solution

Not sure whether we have many Prism specialists on Code Review and I haven't worked with it for a while, so maybe my comments are incorrect at points.

  • I do not really like the way you're handling region.Views.CollectionChanged event. This event provides a lot of information in eventArgs but you're ignoring it. Firstly you do not handle removing views at all. Secondly instead of iterating through all views every time I would use those from eventArgs (NewItems property).



  • I believe generally it is better to have views isolated from regions in terms of concrete types. Your view is not isolated since it has to inherit RibbonTab class. Maybe it is not an issue here and I'm not sure whether it should be changed somehow, it just looks strange to me.

Context

StackExchange Code Review Q#429, answer score: 7

Revisions (0)

No revisions yet.