XamTabControlRegionAdapter

May 26, 2009 at 1:37 PM

For those interested, here is a Region Adapter for the Infragistics XamTabControl.  It's dead simple since it only adds support for the XamTabControl close button removing the associated View from the Region.  It extends the SelectorRegionAdapter.  It's done in VB since that is what my client targets.

If there is sufficient interest and laziness, I'll convert it to C#.

Here is the usage:

<igWindows:XamTabControl cal:RegionManager.RegionName="SomeTabbedRegion" RegionAdapters:XamTabControLRegionAdapter.RemoveOnClose="True" AllowTabClosing="True" />

... and the code ... 

 
Imports Infragistics.Windows.Controls
Imports Microsoft.Practices.Composite.Regions
Imports Microsoft.Practices.Composite.Presentation.Regions
Imports System.Windows.Controls.Primitives

Namespace RegionAdapters
  Public Class XamTabControlRegionAdapter
    Inherits Microsoft.Practices.Composite.Presentation.Regions.SelectorRegionAdapter


    Public Sub New(ByVal regionBehaviorFactory As IRegionBehaviorFactory)
      MyBase.New(regionBehaviorFactory)
    End Sub


#Region "RemoveOnClose"

    ''' <summary>
    ''' RemoveOnClose Attached Dependency Property
    ''' </summary>
    Public Shared ReadOnly RemoveOnCloseProperty As DependencyProperty = _
        DependencyProperty.RegisterAttached("RemoveOnClose", GetType(Boolean), GetType(XamTabControlRegionAdapter), _
            New FrameworkPropertyMetadata(False))

    ''' <summary>
    ''' Gets the RemoveOnClose property.  This dependency property
    ''' indicates ....
    ''' </summary>
    Public Shared Function GetRemoveOnClose(ByVal d As DependencyObject) As Boolean
      Return CType(d.GetValue(RemoveOnCloseProperty), Boolean)
    End Function

    ''' <summary>
    ''' Sets the RemoveOnClose property.  This dependency property
    ''' indicates ....
    ''' </summary>
    Public Shared Sub SetRemoveOnClose(ByVal d As DependencyObject, ByVal value As Boolean)
      d.SetValue(RemoveOnCloseProperty, value)
    End Sub

#End Region

    Public Sub RegisterWith(ByVal mappings As RegionAdapterMappings)
      mappings.RegisterMapping(GetType(XamTabControl), Me)
    End Sub

    Protected Overrides Sub Adapt(ByVal region As Microsoft.Practices.Composite.Regions.IRegion, ByVal regionTarget As Selector)
      If (region Is Nothing) Then
        Throw New ArgumentNullException("region")
      End If

      If (regionTarget Is Nothing) Then
        Throw New ArgumentNullException("regionTarget")
      End If

      If Not (TypeOf regionTarget Is XamTabControl) Then
        Throw New ArgumentException("XamTabControlRegionAdapter can only be used for XamTabControl targets.")
      End If

      Dim xamTabControl = CType(regionTarget, XamTabControl)

      ' Did they specify that they wanted this bad boy removed?
      Dim removeOnClose = GetRemoveOnClose(xamTabControl)

      If (removeOnClose) Then
        xamTabControl.AddHandler(TabItemEx.ClosedEvent, New RoutedEventHandler(Function(o, e) OnTabClosed(o, e, region)), True)
      End If

      MyBase.Adapt(region, regionTarget)

    End Sub

    Protected Overrides Function CreateRegion() As Microsoft.Practices.Composite.Regions.IRegion
      Return New SingleActiveRegion()
    End Function

    Private Function OnTabClosed(ByVal o As Object, ByVal e As RoutedEventArgs, ByVal region As IRegion) As Object

      If (TypeOf e.OriginalSource Is TabItemEx) Then
        Dim tabItem = CType(e.OriginalSource, TabItemEx)

        If (tabItem.Content IsNot Nothing) Then
          region.Remove(tabItem.Content)
        End If
      End If

      Return Nothing
    End Function

  End Class
End Namespace

 

 

Jul 5, 2010 at 2:20 AM

Hello,

    Would you send me the C# Code ? Thank you very much.

 

 

Jul 6, 2010 at 12:42 PM

Sorry, only needed for a VB project.  You are more than welcome to convert the above code into C# or build a VB assembly and reference that.  You can always get a head start with something like the devFusion converter: http://www.developerfusion.com/tools/convert/vb-to-csharp/

 

Jul 7, 2010 at 3:35 AM

Thank you , I will have a try.