• Prism v2.1: Creating a Region Adapter for the Accordion control

    Published by Damian Schenkelman on March 18th, 2010 3:47 pm under Composite Application Guidance for WPF & SL, Patterns & Practices, Prism-v2, Prism-v2.1, Silverlight, Silverlight 3, p&p

    4 Comments

    Today with Guido Maliandi, we set to find a way to use the Accordion from the Silverlight toolkit as region in a Prism application. As the Accordion inherits from ItemsControl, Prism already provided some functionality to get that control working as a region. However, we wanted it to have the following behavior:

    • Only one view in the accordion region should be active at a time.
    • It should be possible to set the header for each AccordionItem.
    • It should be easy to extend with other required functionality.

    If you are familiar with Prism’s source code, you might probably notice that is quite similar to the way Prism handles Silverlight’s TabControl as a region. Our approach was creating two classes:

    • AccordionRegionAdapter: This class would be in charge of creating the region and hooking it up with the control.
    • AccordionRegionSyncBehavior: This class would provide the region’s functionality.

    Any other functionality that was necessary could be added through other RegionBehaviors. This document from the Prism documentation explains the usual process of creating a custom Region Adapter.

    As the code for both those classes is quite long, I decided to simply provide the download at the bottom of this post.

    Setting the AccordionItem’s Header Text

    As it can be done with the TabControl region, the following syntax can be used to show some text in the header of each AccordionItem (in this case the View’s DataContext is simply a string). The outcome can be seen in the picture placed after the code.

    <tk:Accordion Name="MainRegion" Regions:RegionManager.RegionName="MainRegion">
      <shell:AccordionRegionAdapter.ItemContainerStyle>
        <Style TargetType="tk:AccordionItem">
          <Setter Property="HeaderTemplate">
            <Setter.Value>
              <DataTemplate>
                <TextBlock Text="{Binding}"/>
              </DataTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      </shell:AccordionRegionAdapter.ItemContainerStyle>
    </tk:Accordion>
    
    Sample Application

    image

    I created a small sample application which shows this scenario working. You can download it from here. The code is provided “AS IS” with no warranties and confers no rights.

    I hope you can make good use of it.

    Shout it

    Tags: , , , , , , ,

  • 4 Comments:

    1. DotNetShoutout said on March 18, 2010:

      Prism v2.1: Creating a Region Adapter for the Accordion control…

      Thank you for submitting this cool story – Trackback from DotNetShoutout…

    2. iAwaaz-News-by-People said on March 19, 2010:

      Prism v2.1: Creating a Region Adapter for the Accordion control…

      Thank you for submitting this cool story – Trackback from iAwaaz-News-by-People…

    3. uberVU - social comments said on March 20, 2010:

      Social comments and analytics for this post…

      This post was mentioned on Twitter by clientdev: RT @dschenkelman: just blogged: Prism v2.1: Creating a Region Adapter for the Accordion control http://is.gd/aNV24...

    4. [Silverlight introduction series] to the certification candidates at every level of the Accordion Accordion embedded TreeView said on June 15, 2011:

      [...] if you use is Prism, and to the control to write a RegionAdapter, please referenceThis articleandThis articleThe article, if you are using a MEF not Unity, to do the following [...]

    Leave a comment

    Your email address will not be published.

Categories

Archives

Map

SO Profile

Client Dev Twitter