Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Collapse]eXpressApp Framework
 [Expand]Fundamentals
 [Expand]Getting Started
 [Expand]Concepts
 [Expand]Design-Time Features
 [Expand]Functional Testing
 [Expand]Deployment
 [Collapse]Task-Based Help
  [Expand]Business Model Design
  [Expand]Application Model
  [Expand]Actions
  [Expand]Navigation
  [Collapse]Views
    How to: Access Objects Selected in the Current View
    How to: Access the Master Object from a Nested List View
    How to: Create and Show a Detail View of the Selected Object in a Popup Window
    How to: Detect a Lookup List View in Code
    How to: Display a Detail View Directly in Edit Mode in ASP.NET Applications
    How to: Display a List View as a Chart
    How to: Display Several Views Side-by-Side
    How to: Hide Collection Properties in an Edit Mode Detail View for an ASP.NET Application
    How to: Implement a Singleton Business Object and Show its Detail View
    How to: Implement a View Item
    How to: Show a Custom Data-Bound Control in an XAF View (ASP.NET)
    How to: Show a Custom Data-Bound Control in an XAF View (WinForms)
  [Expand]List Editors
  [Expand]Property Editors
  [Expand]Templates
  [Expand]Filtering
  [Expand]Reporting
  [Expand]Dashboards
  [Expand]Scheduler and Notifications
  [Expand]Maps
  [Expand]Security
  [Expand]Workflow
  [Expand]Localization
  [Expand]Testing
  [Expand]Miscellaneous UI Customizations
 [Expand]Frequently Asked Questions
 [Expand]API Reference
[Expand]CodeRush
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

How to: Access Objects 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.

Show Me

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E3016.

Expanded Access Currently Selected Objects When an Action is Executed

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.

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.

Note

With the code above, each selected Contact object is obtained through a separate database request.

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.

Expanded Access Currently Selected Objects with a View Controller

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.

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.

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.

How would you rate this topic?​​​​​​​