zoukankan      html  css  js  c++  java
  • 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.

    使用 XAF 应用程序时,最终用户可以选择显示在视图中的对象。您可能需要从控制器和操作访问这些对象以执行各种业务任务。例如,在实现操作时,您可能需要访问焦点对象,以在执行 Action 时修改其属性值。本主题介绍操作焦点对象和选定对象的基础知识,并提供示例代码段。

    Tip 提示
    A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E3016
    完整的示例项目可在 DevExpress 代码示例数据库中找到,http://www.devexpress.com/example=E3016

    .

    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 属性值或"详细信息视图"中显示的对象添加新行,显示信息(关于工资转移的时刻)。

    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 += "
    [INFO] Your salary is transfered " + 
                    now.ToString("M/d/yy") + " at " + now.ToString("hh:mm");
            }
            ObjectSpace.CommitChanges();
            ObjectSpace.Refresh();
        }
    }
    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.

    通过指定执行事件处理程序的 ActionBaseEventArgs.ShowViewParameters 属性或使用 PopupwindowShowAction,在执行操作时,可以显示特定的视图。但是,无论操作类型如何,Execute 事件处理程序参数始终包含为其调用操作的视图的有重点和选定对象,而不是作为操作的结果显示的视图。

    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.

    较不常见的任务是从控制器访问视图的集中和选定对象。在这种情况下,应使用视图.当前对象和视图.选定对象由视图控制器.View 属性指定的视图对象的属性。视图对象公开的属性具有相应的更改通知事件 - 视图.当前对象更改和视图.选择更改。因此,从控制器访问焦点对象和选定对象的最佳方法是处理这些事件。

    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.

    以下代码段演示了用于联系人详细信息视图的控制器。它更改删除对象视图控制器.删除操作的操作库.确认消息。如果要删除一个联系人,将删除的联系人的全名将添加到确认消息中。如果要删除多个联系人,将改为添加选定的"联系人"计数。

    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 注意
    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.
    当视图在数据视图模式(和 XafInstant反馈记录 - 在即时反馈模式下)运行时,视图当前对象和视图。所选对象属性返回 XafDataViewRecord 记录对象,而不是原始业务对象。要获取真实对象,请使用 View.ObjectSpace.GetObject(obj)方法。
  • 相关阅读:
    精确覆盖DLX算法模板另一种写法
    Hdu3498-whosyourdaddy(精确覆盖模板题)
    精确覆盖DLX算法模板
    Object2Map
    Use ResourceBundle read properties file.
    Kendo UI Example(grid)
    Kendo 日期控件
    Spring mvc 中文乱码问题解决方法
    Thread communication java.util.concurrent.locks.Condition 用法(二)
    Thread communication java.util.concurrent.locks.Condition 用法(一)
  • 原文地址:https://www.cnblogs.com/foreachlife/p/How-to-Access-Objects-Selected-in-the-Current-View.html
Copyright © 2011-2022 走看看