The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.

How to: Create a New Object using the Navigation Control

  • 4 min 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 http://www.devexpress.com/example=E236.

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;
using DevExpress.Xpo;
// ...
[DefaultClassOptions, ImageName("BO_List")]
public class Issue : BaseObject {
    public Issue(Session session) : base(session) { }
    private string subject;
    public string Subject {
        get { return subject; }
        set { SetPropertyValue(nameof(Subject), ref subject, value); }
    }
    private string description;
    [Size(SizeAttribute.Unlimited)]
    public string Description {
        get { return description; }
        set { SetPropertyValue(nameof(Description), ref description, value); }
    }
}

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

Note

Do not set the StartupNavigationItem property to this navigation item for Mobile applications, because they do not support a Detail View displaying on an application start.

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 or Mobile 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).