How to: Initialize an Object Created Using the New Action How to: initialize objects created using the new button

This topic describes how to access an object that is created using the New Action. Assume you are using the Task business class from the Business Class Library. When creating a new Task using the New Action, the Task.StartDate property will be set to the current date.

This topic describes how to access the object using the "New" button to create. Suppose you are using business class library Task business class. Use the "New" button to create a new task, "Task.StartDate" attribute set to the current date.

Note Note
The approach described in this topic is not supported by the Mobile platform. If it is necessary to implement this scenario in your Mobile application, contact us using the Support Center
Mobile platform approach described in this topic is not supported. If you need to implement this program in a mobile application, please contact us using Support Center

.

Tip Tip
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E229
Complete sample project can be found in the code sample database DevExpress, http://www.devexpress.com/example=E229

.

To access an object created using the New Action, handle the NewObjectViewController.ObjectCreated event of the NewObjectViewController that contains the New Action. To do this, implement a new View Controller. Override the Controller's OnActivated method and subscribe to the ObjectCreated event in the following manner:

To access the object using the "New Action" to create, use the "new view controller objects. Objects created" NewObjectViewController event handling include "New operation". For this reason, to achieve a new view controller. On rewrite controller method has been activated, and then in the following manner subscribe ObjectCreated events:

using DevExpress.Persistent.BaseImpl;
using DevExpress.ExpressApp.SystemModule;
//...
public class MyController : ViewController {
    private NewObjectViewController controller;
    protected override void  OnActivated() {
        base.OnActivated();
        controller = Frame.GetController<NewObjectViewController>();
        if (controller != null) {
            controller.ObjectCreated += controller_ObjectCreated;
        }
    }
    void controller_ObjectCreated(object sender, ObjectCreatedEventArgs e) {
        if (e.CreatedObject is Task) {
            ((Task)e.CreatedObject).StartDate = DateTime.Now;
        }
    }
    protected override void OnDeactivated() {
        if (controller != null) {
            controller.ObjectCreated -= controller_ObjectCreated;
        }
        base.OnDeactivated();
    }
}

 

In certain scenarios, it can be required to initialize a new object created through the lookup editor's New button, using a value from the parent Detail View. To access the parent object from the ObjectCreated event handler, cast the Controller.Frame value to the NestedFrame type, access the NestedFrame.ViewItem property and then get the master object using the ViewItem.CurrentObject property.

In some cases, a parent may need to use the new object initialization value Details view created by looking for the editor of the "New" button. Object created from the event handler can access the parent object, the controller .Frame value into a nested frame type, access NestedFrame.ViewItem property, and then use ViewItem.CurrentObject property to get the main object.

void controller_ObjectCreated(object sender, ObjectCreatedEventArgs e) {
    NestedFrame nestedFrame = Frame as NestedFrame;
    if (nestedFrame != null) {
        Item createdItem = e.CreatedObject as Item;
        if (createdItem != null) {
            Parent parent = ((NestedFrame)Frame).ViewItem.CurrentObject as Parent;
            if (parent != null) {
                createdItem.Title = parent.DefaultItemTitle;
            }
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/foreachlife/p/How-to-Initialize-an-Object-Created-Using-the-New-Action.html