How to: Access Objects Selected in the Current View How to: Object Access selected in the current view

When working with XAF applications, end-users can select objects displayed in a View. You may often need to access these objects from Controllers and Actions to perform various business tasks. For example, when implementing an Action, you may need to access a focused object to modify its property values when an Action is executed. This topic explains the basics of manipulating focused and selected objects, and provides sample code snippets.

When using XAF application, the end user may select an object displayed in the view. You may need to access these objects from the controller to perform various operations and business tasks. For example, in the realization of the operation, you may need to access the object focus, to modify its property values ​​in the implementation of Action. This topic describes the basics of operating the focus of the object and the selected object, and provides sample code snippets.

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

.

Access Currently Selected Objects When an Action is Executed

Access the currently selected object while performing the operation

When an Action is executed, its Execute event is triggered. Regardless of the Action type, arguments passed to the event handler contain the SimpleActionExecuteEventArgs.CurrentObject and SimpleActionExecuteEventArgs.SelectedObjects properties. The CurrentObject property specifies the current object of the active View. If an active View is a List View, this property specifies the focused object. If the View is a Detail View, the property specifies the object displayed by it. The SelectedObjects is a collection of the objects selected in the active View. In the case of a Detail View, this property returns the CurrentObject wrapped in a list.

When performing operations, its implementation will trigger event. How to type, pass a parameter to the event handler, whether the operation contains "simple implementation of event" attribute. Current object "current object" attribute specifies the active view. If the active view is a list view, this attribute specifies the focus object. If the "view" is the "detailed view", the display of the specified object property. "Selected Object" is a collection of activities in view of the selected object. In the case of a detailed view of the package of this property returns the current object in the list.

 

The following code snippet demonstrates an Action intended for a Contact type. When the Action is executed, it adds a new line displaying information (about the moment when salary is transferred) to the Note property value of the currently selected objects in a List View, or an object displayed in a Detail View.

The following code snippet demonstrates the type of operation for the contact. When performing operations, it currently selected list view Note to the property value of the object or "Detail View" in the subject line to add a new display, displays information (about the time of the transfer of wages).

using System;
using System.Collections;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.EF;
//...
public partial class MyNotesController : ViewController {
    public MyNotesController() {
        SimpleAction myAction = new SimpleAction(this, "Salary Info", "Edit");
        myAction.SelectionDependencyType = SelectionDependencyType.RequireMultipleObjects;
        myAction.TargetObjectType = typeof(Contact);
        myAction.Execute += myAction_Execute;
        Actions.Add(myAction);
    }
    void myAction_Execute(object sender, SimpleActionExecuteEventArgs e) {
        ArrayList SelectedContacts = new ArrayList();
        if ((e.SelectedObjects.Count > 0) && 
            ((e.SelectedObjects[0] is XafDataViewRecord) || (e.SelectedObjects[0] is XafInstantFeedbackRecord))) {
            foreach (var selectedObject in e.SelectedObjects) {
                SelectedContacts.Add((Contact)ObjectSpace.GetObject(selectedObject));
            }
        }
        else {
            SelectedContacts = (ArrayList)e.SelectedObjects;
        }
        foreach (Contact selectedContact in SelectedContacts) {
            DateTime now = DateTime.Now;
            selectedContact.Notes += "\r\n[INFO] Your salary is transfered " + 
                now.ToString("M/d/yy") + " at " + now.ToString("hh:mm");
        }
        ObjectSpace.CommitChanges();
        ObjectSpace.Refresh();
    }
}

 

Note Note
With the code above, each selected Contact object is obtained through a separate database request.
Using the above codes, each request to obtain the selected contact object by a separate database.

A specific View can be displayed when an Action is executed by specifying the ActionBaseEventArgs.ShowViewParameters property of the Execute event handler or by using a PopupWindowShowAction. However, regardless of the Action type, the Execute event handler arguments always contain focused and selected objects of the View for which the Action was invoked, and not for the View that was displayed as a result of the Action.

By performing the specified event handler attributes or ActionBaseEventArgs.ShowViewParameters PopupwindowShowAction, when performing the operation, a particular view may be displayed. However, regardless of the type of operation, Execute event handler parameters always include the view is called for a focused and operation of selected objects, rather than as a result of the operation displayed in the view.

Access Currently Selected Objects with a View Controller

Use the view controller accesses the currently selected object

A less common task is accessing focused and selected objects of a View from a Controller. In this instance, you should use the View.CurrentObject and View.SelectedObjects properties of the View object specified by the ViewController.View property. The properties exposed by the View object have corresponding change notification events - View.CurrentObjectChanged and View.SelectionChanged. So the best approach to accessing the focused and selected objects from a Controller is to handle these events.

Less common tasks from a centralized controller to access and view the selected objects. In this case, use view objects and view the current. Properties of the selected object specified by the view controller .View view object attributes. Open view object attributes has a corresponding change notification events - view and change current object view selection changes... Therefore, the best way to access the object and the focus of the selected objects from the controller is to deal with these events.

 

The following code snippet demonstrates a Controller intended for Contact Detail Views. It changes the DeleteObjectsViewController.DeleteAction's ActionBase.ConfirmationMessage. If you are going to delete one contact, the FullName of the Contact that is going to be deleted will be added to the ConfirmationMessage. If you want to delete several contacts, the selected Contacts count will be added instead.

The following code segment illustrates the controller for the contact details of view. It changes the deleted object view controller. Deletion library operation. Acknowledgment message. If you want to delete a contact, the full name deleted contacts added to the confirmation message. If you want to delete multiple contacts, will be changed to add the selected "Contacts" count.

public class MyConfirmationController : ViewController {
    private string defaultMessage;
    DeleteObjectsViewController deleteObjectsViewController;
    public MyConfirmationController() {
        this.TargetObjectType = typeof(Contact);
    }
    protected override void OnActivated() {
        base.OnActivated();
        deleteObjectsViewController = Frame.GetController<DeleteObjectsViewController>();
        if (deleteObjectsViewController != null) {
            defaultMessage = deleteObjectsViewController.DeleteAction.GetFormattedConfirmationMessage();
            View.SelectionChanged += View_SelectionChanged;
            UpdateConfirmationMessage();                
        }
    }
    void View_SelectionChanged(object sender, System.EventArgs e) {
        UpdateConfirmationMessage();
    }
    private void UpdateConfirmationMessage() {
        if (View.SelectedObjects.Count == 1) {
            deleteObjectsViewController.DeleteAction.ConfirmationMessage =
                String.Format("You are about to delete the '{0}' Contact. Do you want to proceed?",
                ((Contact)View.CurrentObject).FullName);
        }
        else {
            deleteObjectsViewController.DeleteAction.ConfirmationMessage =
                String.Format("You are about to delete {0} Contacts. Do you want to proceed?",
                (View.SelectedObjects.Count));
        }
    }
    protected override void OnDeactivated() {
        base.OnDeactivated();
        if (deleteObjectsViewController != null) {
            View.SelectionChanged -= View_SelectionChanged;
            deleteObjectsViewController.DeleteAction.ConfirmationMessage = defaultMessage;
            deleteObjectsViewController = null;
        }
    }
}

 

Note Note
The View.CurrentObject and View.SelectedObjects properties return XafDataViewRecord objects instead of original business objects when the View operates in the DataView mode (and XafInstantFeedbackRecord - in InstantFeedback mode). To get the real object, use the View.ObjectSpace.GetObject(obj) method.
When the view data in the view mode (recording and XafInstant feedback - in immediate feedback mode), the current view and the view objects. Property returns XafDataViewRecord selected object record object, instead of the original business object. To get the real object, use View.ObjectSpace.GetObject (obj) method.

Guess you like

Origin www.cnblogs.com/foreachlife/p/How-to-Access-Objects-Selected-in-the-Current-View.html