Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WPF

Region Adapters for Infragitics

0.00/5 (No votes)
29 May 2012CPOL 9.6K  
Region adapters in VB.NET

This article appears in the Third Party Products and Tools section. Articles in this section are for the members only and must not be used to promote or advertise products in any way, shape or form. Please report any spam or advertising.

Introduction

Creating Prism 4 region adapters can be hard when using 3rd party controls. Infragistics develop superb WPF controls however there are not many snippets out there that cover region adapters for Prism.

This tip states how to develop a region adapter for Infragistics using VB.NET.

The code was developed in VB.NET and Visual Studio 2010. It targets the .NET 4 Framework and works on Infragistics 10.1 WPF controls onwards.  

XamRibbon Region Adapter 

Region adapters are created using the following code:

VB.NET
<pre>Imports System.Collections.Specialized
Imports Infragistics.Windows.Ribbon
Imports Microsoft.Practices.Prism.Regions
Imports System.ComponentModel.Composition

Namespace Adapters
''' <summary>
''' XamRibbonRegionAdapter for the Infragistics XamRibbon
''' </summary>
''' <remarks></remarks>
<Export(GetType(XamRibbonRegionAdapter))>
Public Class XamRibbonRegionAdapter
Inherits RegionAdapterBase(Of XamRibbon)
Private _xamRibbonRegionTarget As XamRibbon = Nothing

''' <summary>
''' Initializes a new instance of the <see cref="XamRibbonRegionAdapter" /> class.
''' </summary>
''' <param name="regionBehaviorFactory">The region behavior factory.</param>
''' <remarks></remarks>
<ImportingConstructor()>
Public Sub New(ByVal regionBehaviorFactory As IRegionBehaviorFactory)
MyBase.New(regionBehaviorFactory)
End Sub

#Region "Overrides"
 '' <summary>
''' Adapts the specified region.
''' </summary>
''' <param name="region">The region.</param>
''' <param name="regionTarget">The region target.</param>
''' <remarks></remarks>
Protected Overrides Sub Adapt(ByVal region As IRegion, ByVal regionTarget As XamRibbon)
_xamRibbonRegionTarget = regionTarget

If _xamRibbonRegionTarget.Tabs.Count > 0 Then
For Each tab As Object In _xamRibbonRegionTarget.Tabs
region.Add(tab)
Next

_xamRibbonRegionTarget.Tabs.Clear()
End If

If _xamRibbonRegionTarget.ContextualTabGroups.Count > 0 Then
For Each tabGroup As Object In _xamRibbonRegionTarget.ContextualTabGroups
region.Add(tabGroup)
Next

_xamRibbonRegionTarget.ContextualTabGroups.Clear()
End If

For Each view As Object In region.Views
AddViewToRegion(view, _xamRibbonRegionTarget)
Next

AddHandler region.ActiveViews.CollectionChanged, _
	Sub(sender As Object, e As NotifyCollectionChangedEventArgs)
Select Case e.Action
Case NotifyCollectionChangedAction.Add
If True Then
For Each view As Object In e.NewItems
AddViewToRegion(view, _xamRibbonRegionTarget)
Next
End If
Case NotifyCollectionChangedAction.Remove
If True Then
For Each view As Object In e.OldItems
RemoveViewFromRegion(view, _xamRibbonRegionTarget)
Next
End If
End Select
End Sub
End Sub

''' <summary>
''' Creates the region.
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Protected Overrides Function CreateRegion() As IRegion
Return New AllActiveRegion()
End Function

#End Region

#Region "Private"
 '' <summary>
''' Adds the view to region.
''' </summary>
''' <param name="view">The view.</param>
''' <param name="xamRibbon">The xam ribbon.</param>
''' <remarks></remarks>
Private Sub AddViewToRegion(ByVal view As Object, ByVal xamRibbon As XamRibbon)
Dim ribbonTabItem As RibbonTabItem = TryCast(view, RibbonTabItem)
If ribbonTabItem IsNot Nothing Then
xamRibbon.Tabs.Add(ribbonTabItem)
Else
Dim contextualTabGroup As ContextualTabGroup = TryCast(view, ContextualTabGroup)
If contextualTabGroup IsNot Nothing Then
xamRibbon.ContextualTabGroups.Add(contextualTabGroup)
End If
End If
End Sub

''' <summary>
''' Removes the view from region.
''' </summary>
''' <param name="view">The view.</param>
''' <param name="xamRibbon">The xam ribbon.</param>
''' <remarks></remarks>
Private Sub RemoveViewFromRegion(ByVal view As Object, ByVal xamRibbon As XamRibbon)
Dim ribbonTabItem As RibbonTabItem = TryCast(view, RibbonTabItem)
If ribbonTabItem IsNot Nothing Then
xamRibbon.Tabs.Remove(ribbonTabItem)
Else
Dim contextualTabGroup As ContextualTabGroup = TryCast(view, ContextualTabGroup)
If contextualTabGroup IsNot Nothing Then
xamRibbon.ContextualTabGroups.Remove(contextualTabGroup)
End If
End If
End Sub
#End Region

End Class
End Namespace 

XamOutlookbar Region Adapter

The following adapter is very similar to the ribbon however this is targeted at the XamOutlookbar.

Imports System.Collections.Specialized
Imports Infragistics.Windows.OutlookBar
Imports Microsoft.Practices.Prism.Regions
Imports System.ComponentModel.Composition

Namespace Adapters
 ''' <summary>
 ''' XamOutlookBarRegionAdapter region adapter for the XamOutlookBar
 ''' </summary>
 ''' <remarks></remarks>
 <Export(GetType(XamOutlookBarRegionAdapter))>
 Public Class XamOutlookBarRegionAdapter
 Inherits RegionAdapterBase(Of XamOutlookBar)
 Private _xamOutlookBarRegionTarget As XamOutlookBar = Nothing

''' <summary>
 ''' Initializes a new instance of the <see cref="XamOutlookBarRegionAdapter" /> class.
 ''' </summary>
 ''' <param name="regionBehaviorFactory">The region behavior factory.</param>
 ''' <remarks></remarks>
 <ImportingConstructor()>
 Public Sub New(ByVal regionBehaviorFactory As IRegionBehaviorFactory)
 MyBase.New(regionBehaviorFactory)
 End Sub

#Region "Overrides"
 Protected Overrides Sub Adapt(ByVal region As IRegion, ByVal regionTarget As XamOutlookBar)
 _xamOutlookBarRegionTarget = regionTarget

If _xamOutlookBarRegionTarget.Groups.Count > 0 Then
 For Each group As Object In _xamOutlookBarRegionTarget.Groups
 region.Add(group)
 Next

_xamOutlookBarRegionTarget.Groups.Clear()
 End If

For Each view As Object In region.Views
 AddViewToRegion(view, _xamOutlookBarRegionTarget)
 Next

AddHandler region.ActiveViews.CollectionChanged, Sub(s, args)
 Select Case args.Action
 Case NotifyCollectionChangedAction.Add
 If True Then
 For Each view As Object In args.NewItems
 AddViewToRegion(view, _xamOutlookBarRegionTarget)
 Next
 End If
 Case NotifyCollectionChangedAction.Remove
 If True Then
 For Each view As Object In args.OldItems
 RemoveViewFromRegion(view, _xamOutlookBarRegionTarget)
 Next
 End If
 End Select
 End Sub
 End Sub

''' <summary>
 ''' Creates the region.
 ''' </summary>
 ''' <returns></returns>
 ''' <remarks></remarks>
 Protected Overrides Function CreateRegion() As IRegion
 Return New AllActiveRegion()
 End Function

#End Region

#Region "Private"
 ''' <summary>
 ''' Adds the view to region.
 ''' </summary>
 ''' <param name="view">The view.</param>
 ''' <param name="xamOutlookBar">The xam outlook bar.</param>
 ''' <remarks></remarks>
 Private Sub AddViewToRegion(ByVal view As Object, ByVal xamOutlookBar As XamOutlookBar)
 Dim groupItem As OutlookBarGroup = TryCast(view, OutlookBarGroup)
 If groupItem IsNot Nothing Then
 xamOutlookBar.Groups.Add(groupItem)
 End If
 End Sub

''' <summary>
 ''' Removes the view from region.
 ''' </summary>
 ''' <param name="view">The view.</param>
 ''' <param name="xamOutlookBar">The xam outlook bar.</param>
 ''' <remarks></remarks>
 Private Sub RemoveViewFromRegion(ByVal view As Object, ByVal xamOutlookBar As XamOutlookBar)
 Dim groupItem As OutlookBarGroup = TryCast(view, OutlookBarGroup)

If groupItem IsNot Nothing Then
 xamOutlookBar.Groups.Remove(groupItem)
 End If
 End Sub
#End Region

End Class
End Namespace

Once these are in place, the last thing you need to do is register the region adapter in the prism bootstrapper.

''' <summary>
''' Configures the region adapter mappings.
 ''' </summary>
 ''' <returns></returns>
 ''' <remarks></remarks>
 Protected Overrides Function ConfigureRegionAdapterMappings() _
 As Microsoft.Practices.Prism.Regions.RegionAdapterMappings
 Dim regionAdapterMappings = MyBase.ConfigureRegionAdapterMappings()

regionAdapterMappings.RegisterMapping(GetType(XamRibbon), _
Container.GetExportedValue(Of XamRibbonRegionAdapter))
 regionAdapterMappings.RegisterMapping(GetType(XamOutlookBar), _
 Container.GetExportedValue(Of XamOutlookBarRegionAdapter))

Return regionAdapterMappings

End Function 

Points of Interest

There are a couple of examples in C# on the infragistics forums however I found none on VB.NET. I hope this tip helps others.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)