expressappframework-113011-app-shell-and-base-infrastructure-view-variants-module.md
The XAF allows View customization at design time, in code, and by end-users. You may wish to provide several customized variants of the same View, and allow an end-user to choose the desired View Variant at runtime. For instance, end-users may need to use different List View column sets and Detail View layouts in different scenarios. They may also need to use two List View modes: a single List View and a List View with a Detail View (see IModelListView.MasterDetailMode). XAF offers built-in implementation of this feature in the ViewVariantsModule, which is declared in the DevExpress.ExpessApp.ViewVariantsModule.v25.2.dll assembly. This topic will show you how to use this module to provide multiple versions of the same View. The following steps explain how to apply this module’s capabilities.
To apply the View Variants module capabilities, add this module to your application project. For this purpose, use the Application Designer or Module Designer. Be sure to rebuild your solution after making changes in the Designer.
Note
In XAF applications, you can call the AddViewVariants<TBuilder>(IModuleBuilder<TBuilder>, Action<ViewVariantsOptions>) method in your ASP.NET Core Blazor/WinForms application builder.
You can add modules to your application when you use the Template Kit to create a new XAF solution. Select modules in the Additional Modules section.
To add an extra module in code, add it to the XafApplication.Modules or ModuleBase.RequiredModuleTypes list (adding a reference to the module assembly is insufficient).
To see a demonstration of View Variants, you can run the Feature Center application that ships with XAF (the %PUBLIC%\Documents\DevExpress Demos 25.2\Components\XAF\FeatureCenter.NET.XPO folder), and navigate to the View Variants item, or refer to the Feature Center demo online.
The View Variants module provides the ChangeVariantController View Controller. It contains the ChangeVariant Action, which allows end-users to select a View Variant to be displayed within the current Window (Frame). This Action is of the SingleChoiceAction type and has the ChangeVariant ID. Its items are specified by the Views | <View> | Variants node of the Application Model. The action is visible if there are two or more Variants child nodes in the current View node.
To add View Variants, do the following:
The following image illustrates how the varied View and its View Variants are organized in the Application Model.
The image below illustrates the ChangeVariant Action available in a WinForms application after following the steps above.
A variant can be chosen from the drop-down list. Variants are sorted according to their IModelNode.Index property values. Additionally, you can set the Current property of the Variants node to specify the default variant. The previously selected variant ID is stored with user customizations (in the Model.User.xafml file or in browser cookies).
View Variants can be also added to the Navigation. Set the IModelNavigationItemsVariantSettings.GenerateRelatedViewVariantsGroup property to true in the Model Editor.
The image below illustrates the result.
You can create a custom IVariantsProvider object that loads View Variants in a custom manner. Assign your VariantsProvider to the ViewVariantsModule.VariantsProvider property to replace the default ModelVariantsProvider that loads View Variants from the Application Model.
See Also
Best practices of creating reusable XAF modules by example of a View Variants module extension
XAF - How to save the currently opened View as a new View Variant at runtime