Skip to main content

DevExpress v24.1 Update — Your Feedback Matters

Our What's New in v24.1 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

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.

CreateObjectFromNavigationControl

Tip

A complete sample project is available in the DevExpress Code Examples database at https://supportcenter.devexpress.com/ticket/details/e236/xaf-how-to-show-a-new-object-detail-view-via-the-navigation-control.

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
IModelBaseChoiceActionItem.Id NewIssue
IModelBaseChoiceActionItem.Caption Create New Issue…
IModelBaseChoiceActionItem.ImageName Action_New
IModelNavigationItem.View Issue_DetailView

CreateObjectFromNavigationControl_ME

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:

  1. 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.
  2. In the CustomShowNavigationItem event handler, access the current navigation item identifier using the CustomShowNavigationItemEventArgs.ActionArguments event argument.
  3. If the identifier is “NewIssue”, create the following objects using the corresponding methods from the table below:

    Object Method
    Object Space XafApplication.CreateObjectSpace
    Issue object IObjectSpace.CreateObject
    Detail View for the Issue object XafApplication.CreateDetailView
  4. Set the Detail View’s DetailView.ViewEditMode property to ViewEditMode.Edit and specify that this View should be displayed using the ShowViewParameters.CreatedView property.
  5. 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).