Access the Application Model in Code

The Application Model represents a large data store. It is used to generate the UI and configure the application's behavior. This topic describes how to access and customize the Application Model in code.

You can use the following objects to access the Application Model in code:

Object Property
View View.Model
ActionBase ActionBase.Model
PropertyEditor PropertyEditor.Model
XafApplication XafApplication.Model

These properties return the IModelNode descendant object. This object provides access to the corresponding Application Model node and its properties. The object's Application property represents the root Application Model's Application node. Refer to the Application Model Structure topic for more information.

Application Model's and Controls's Changes

UI does not immediately reflect changes made in the Application Model. You should customize the Application Model before the control used to display the target UI element is created and loaded. If you customize the UI element after its control is created and initialized with default settings, access the control directly. Refer to the Access Editor Settings and Access Grid Control Properties topics for more information. You can also recreate a control with the latest Application Model changes as described in the How to: Apply Application Model Changes to the Current View Immediately topic.

Add and Remove Existing Nodes

Call the parent node's IModelNode.AddNode<NodeType> method to add a node and its IModelNode.Remove method to remove this node. The following code snippet demonstrates how to use these methods. The CreateMyViewNode Action exposed by MyViewController creates the MyClass_ListView_Custom View node. The DeleteMyViewNode Action searches the Application Model for the MyClass_ListView_Custom View node and removes it.

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.Base;
//...
public class MyViewController : ViewController {
    public MyViewController() {
        SimpleAction createMyViewNodeyViewNode = new SimpleAction(this, "CreateMyViewNode", PredefinedCategory.Edit);
        createMyViewNodeyViewNode.Execute += createMyViewNodeyViewNode_Execute;
        SimpleAction deleteMyViewNode = new SimpleAction(this, "DeleteMyViewNode", PredefinedCategory.Edit);
        deleteMyViewNode.Execute += deleteMyViewNode_Execute;
    }
    void createMyViewNodeyViewNode_Execute(object sender, SimpleActionExecuteEventArgs e) {
        IModelListView myViewNode = 
            View.Model.Application.Views.AddNode<IModelListView>("MyClass_ListView_Custom");
        myViewNode.ModelClass = View.Model.Application.BOModel.GetClass(typeof(MyClass));
    }
    void deleteMyViewNode_Execute(object sender, SimpleActionExecuteEventArgs e) {
        IModelView myViewNode = View.Model.Application.Views["MyClass_ListView_Custom"];
        if (myViewNode != null)
            myViewNode.Remove();
    }
}
Tip

The code examples above make changes in the top layer of the Application Model (the end-user customizations layer). Use the approaches from the Extend and Customize the Application Model in Code topic to customize the underlying zero layer.

See Also