How to: Create a New Object using the Navigation Control
- 4 minutes to read
This topic demonstrates how to execute custom code on a specific navigation item click. A navigation item that invokes the Detail View in Edit mode for an Issue object is added to the Navigation control.
Tip
A complete sample project is available in the DevExpress Code Examples database at https://supportcenter.
In this example, the following Issue persistent class is used for demo purposes. You can use any other persistent class.
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
using System.ComponentModel.DataAnnotations;
// ...
[DefaultClassOptions, ImageName("BO_List")]
public class Issue : BaseObject {
public virtual string Subject { get; set; }
[FieldSize(FieldSizeAttribute.Unlimited)]
public virtual string Description { get; set; }
}
// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.
To create a new object using the Navigation control, add a new navigation item and specify the code to be executed on this item click.
#Add a new Navigation Item
To add a new navigation item, invoke the Model Editor for the module project by double-clicking the Model.DesignedDiffs.xafml file. Find the Issue navigation item node and add a new node to the same navigation group.
For this node, specify the properties from the table below with the corresponding values:
Property | Value |
---|---|
IModel |
New |
IModel |
Create New Issue… |
IModel |
Action_New |
IModel |
Issue_Detail |
Refer to the Add an Item to the Navigation Control topic to learn more about creating navigation items in the Model Editor.
#Specify the Code to be Executed on a Navigation Item Click
To specify the code to be executed when the Create New Issue… navigation item is clicked, follow the steps below:
- Create a Controller that is the WindowController descendant, override the OnActivated method, and subscribe to the ShowNavigationItemController.CustomShowNavigationItem event in this method. Use the Frame.GetController<ControllerType> method to access the ShowNavigationItemController instance.
- In the CustomShowNavigationItem event handler, access the current navigation item identifier using the CustomShowNavigationItemEventArgs.ActionArguments event argument.
If the identifier is “NewIssue”, create the following objects using the corresponding methods from the table below:
Object Method Object Space Xaf Application. Create Object Space Issue object IObject Space. Create Object Detail View for the Issue object Xaf Application. Create Detail View - Set the Detail View’s DetailView.ViewEditMode property to ViewEditMode.Edit and specify that this View should be displayed using the ShowViewParameters.CreatedView property.
- Since the Navigation control is displayed in the main Window only, the created Controller should be activated for the main Window as well by setting the WindowController.TargetWindowType property to Main in the Controller’s constructor.
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.SystemModule;
// ...
public class NewObjectFromNavigationController : WindowController {
public NewObjectFromNavigationController() {
TargetWindowType = WindowType.Main;
}
protected override void OnActivated() {
base.OnActivated();
ShowNavigationItemController showNavigationItemController = Frame.GetController<ShowNavigationItemController>();
showNavigationItemController.CustomShowNavigationItem += showNavigationItemController_CustomShowNavigationItem;
}
void showNavigationItemController_CustomShowNavigationItem(object sender, CustomShowNavigationItemEventArgs e) {
if (e.ActionArguments.SelectedChoiceActionItem.Id == "NewIssue") {
IObjectSpace objectSpace = Application.CreateObjectSpace(typeof(Issue));
Issue newIssue = objectSpace.CreateObject<Issue>();
DetailView detailView = Application.CreateDetailView(objectSpace, newIssue);
detailView.ViewEditMode = DevExpress.ExpressApp.Editors.ViewEditMode.Edit;
e.ActionArguments.ShowViewParameters.CreatedView = detailView;
e.Handled = true;
}
}
}
Run the WinForms, ASP.NET Web Forms, or ASP.NET Core Blazor application to check that the Issue objects can be created using the Create New Issue… navigation item (see the image at the beginning of this topic).