Ways to Show a View
- 6 minutes to read
Typically, Views are displayed in response to user input, e.g., when a menu item is clicked. This topic lists the approaches you can use to create and show a View.
Show a View from the Navigation
The navigation system is visualized by the navigation control, which lists all available Views and provides means to activate the required View. The navigation structure defines the Views order and hierarchy.
Add a View to the Navigation in Code
The simplest way to add a List View to the navigation is to apply the DefaultClassOptionsAttribute attribute to the business class. As a result, a new navigation item is added to the Default group.
You can also use the NavigationItemAttribute for the same purpose. The difference is that this attribute allows you to specify the navigation group, while the DefaultClassOptions attribute always adds an item to the Default group.
You can use the DefaultClassOptions and NavigationItem attributes to show List Views only. Proceed to see how to show other View types.
Add a View to the Navigation in the Designer
You can use the Model Editor to add a List View, Detail View or a Dashboard View to the navigation. The NavigationItems node defines the tree-like structure of navigation items using IModelNavigationItem child nodes. Create a new NavigationItem node within the existing hierarchy and set the IModelNavigationItem.View property to the target List View to show this View from navigation.
To show a Detail View from navigation, you should additionally specify the IModelNavigationItem.ObjectKey property value.
A complete example is available in the Add an Item to the Navigation Control tutorial.
Show a View Using an Action
You can show a specific View when a user clicks a custom Action. Technically, the navigation system is also an Action (see ShowNavigationItemController.ShowNavigationItemAction). When you show a View from navigation, this View is created automatically. However, when you use a custom Action, you will require one of the following methods to create a View object.
- XafApplication.CreateListView - to create a ListView.
- XafApplication.CreateDetailView - to create a DetailView.
- XafApplication.CreateDashboardView - to create a DashboardView.
The View can be shown before an Action execution (e.g., to collect user input required to proceed), and after the execution (e.g., to display certain resulting data).
Show a View Before an Action’s ‘Execute’ Event Occurs
If you are required to show a View before an Action is executed, use the PopupWindowShowAction Action. In this Action type, the PopupWindowShowAction.CustomizePopupWindowParams event is triggered first. Then, the pop-up window is displayed. Finally, the PopupWindowShowAction.Execute event is raised when the OK button is clicked in the pop-up. You can handle the CustomizePopupWindowParams event to create and configure the displayed View.
private void popupWindowShowAction1_CustomizePopupWindowParams(
object sender, CustomizePopupWindowParamsEventArgs e) {
e.View = Application.CreateListView(typeof(Note), true);
}
The complete example for List View is available in the Add an Action that Displays a Pop-Up Window article. For an example of how to create and show a Detail View, refer to the How to: Create and Show a Detail View of the Selected Object in a Popup Window topic.
Show a View After an Action is Executed
In this case, you can use one of the following Action types.
Handle the Action’s Execute event, access the ActionBaseEventArgs.ShowViewParameters argument and pass the View to the ShowViewParameters.CreatedView property.
private void simpleAction1_Execute(object sender, SimpleActionExecuteEventArgs e) {
IObjectSpace objectSpace = Application.CreateObjectSpace(typeof(Person));
e.ShowViewParameters.CreatedView = Application.CreateListView(objectSpace, typeof(Person), true);
}
The ShowViewParameters object also allows configuring other parameters of the displayed View, such as the target window type, template type and associated Controllers. For example, you can specify that the View passed to the ShowViewParameters.CreatedView property should be shown in a pop-up window. Refer to the ShowViewParameters class topic for additional information.
If you want to display a simple text notification on executing the Action, use the ShowViewStrategyBase.ShowMessage method.
Note
The PopupWindowShowAction also passes the ShowViewParameters.CreatedView parameter to its Execute event handler. A View passed to this property will be shown when the DialogOK Action is executed in the pop-up window.
To learn more about using these Actions, refer to the Add an Action with Option Selection and Add a Parametrized Action topics.
Show a View from a Custom Context
Showing a View using an Action is the most convenient and recommended approach. However, in certain rare scenarios, you may be required to show a View that is not associated with any Action. In this instance, create a View using the XafApplication.CreateListView, XafApplication.CreateDetailView or XafApplication.CreateDashboardView method. Then, you can pass the created View object to one of the following methods.
- Frame.SetView - to show the View within the current Frame.
- ShowViewStrategyBase.ShowViewInPopupWindow - to show the View in a pop-up dialog with OK and Cancel buttons.
Non-Persistent Views Specifics
Views of non-persistent objects can be shown using the same techniques. However, you should manually provide data for these views before showing them. Examples:
- How to: Display a List of Non-Persistent Objects in a Popup Dialog
- How to: Display a Non-Persistent Object’s Detail View from the Navigation
- How to: Display a Non-Persistent Object’s List View from the Navigation
Show a Custom non-XAF Form
You can design a custom form or user control in Visual Studio and add them to your XAF application. In most cases, you can create a Controller and show the form on an Action‘s Execute event.
For more information, refer to the following articles:
- How to: Show a Custom Windows Form
- How to: Show a Custom Window with an Embedded XAF View
- How to: Show a Custom Data-Bound Control in an XAF View (ASP.NET Web Forms)
- How to: Show a Custom Data-Bound Control in an XAF View (WinForms)
- Using a Custom Control that is not Integrated by Default
- Ways to Show a Confirmation Dialog