How to: Add a Region
A region is a mechanism that allows developers to expose to the application Windows Presentation Foundation (WPF) controls as components that encapsulate a particular visual way of displaying views. Regions can be accessed in a decoupled way by their name, and they support dynamically adding or removing views at run time.
By showing controls through regions, you can consistently display and hide the views, independently of the visual style in which they display. By doing this, the appearance and layout of your application can evolve independently of the views hosted within it.
This topic describes how to add a region to a view or the Shell window through XAML. For information about how to access a region through code, see How to: Show a View in a Region Using View Injection UI Composition and How to: Show a View in a Scoped Region.
|For more information about regions, see the UI Composition technical concept.|
This topic assumes that the Shell class has a region manager instance registered. If you are working on a solution created with the Composite Application Library, the Shell already contains a region manager instance. For information about how to create a solution, see How to: Create a Solution Using the Composite Application Library.
The following procedure describes how to add a region to a view or the Shell window.
To add a region to a view or the Shell window
- Open the XAML view of your view or the Shell window.
- Add the Composite Application Library XML namespace to the root element of the XAML file, as shown in the following markups for WPF and Silverlight:
- For WPF views, add the following namespace.
- For Silverlight views, add the following namespace.
After this step, the resulting XAML of a WPF view should look similar to the following example markup.
- Add the control you want to use as a region. The Composite Application Library provides three region adapters:
- ContentControlRegionAdapter. This adapter adapts controls of type System.Windows.Controls.ContentControl and derived classes.
- SelectorRegionAdapter. This adapter adapts controls derived from the class System.Windows.Controls.Primitives.Selector, such as the System.Windows.Controls.TabControl control.
- ItemsControlRegionAdapter. This adapter adapts controls of type System.Windows.Controls.ItemsControl and derived classes.
- Add an attribute named RegionManager.RegionName to the control. This attribute is an attached property that indicates that a region has to be created and associated with the control when the view is being built. For the attribute value, provide a name for the region. Developers will use this name to get a reference to the region. Make sure you are using the appropriate namespace for this attached property:
- The following code shows a region named "MainRegion" associated to an ItemsControl control in WPF.
- The following code shows how to associate a region named "MainRegion" to an ItemsControl control in Silverlight.
A recommended practice consists of using constants for region names, as shown in the following markup extracted from the View Injection Composition QuickStart.
Note: You cannot use constants in XAML if you are developing a Silverlight view. Note: The region will be registered with the region manager that corresponds to the scope of the view. For more information about scoped regions, see How to: Show a View in a Scoped Region.
After you add a region, you can get a reference to it by its name and then add views to it.
After you add a region, a typical task to perform next is to add views to it. For details about how to do this, seeHow to: Show a View in a Scoped Region.
For information related to regions, see the following topics:
- How to: Create a Custom Region Adapter
- How to: Show a View in a Region using View Injection UI Composition
- How to: Show a View in a Region Using View Discovery UI Composition
- How to: Show a View in a Scoped Region
For a complete list of How-to topics included with the Composite Application Guidance, see Development Activities.