List Views can be accompanied by Actions that represent features specific to these List Views. In addition to these Actions, every List View has an invisible default Simple Action. In Windows Forms applications, this Action is executed when pressing the ENTER key or double-clicking a selected object. In ASP.NET Web applications, this Action is executed when an object is clicked. This Action is specified by the ListViewProcessCurrentObjectController's ListViewProcessCurrentObjectController.ProcessCurrentObjectAction property. You can replace this Action with a custom Simple Action. This topic demonstrates how to do this.
列表视图可以附带表示特定于这些列表视图的功能的操作。除了这些操作之外,每个列表视图都有一个不可见的默认"简单操作"。在 Windows 窗体应用程序中,按下 ENTER 键或双击选定对象时执行此操作。在 Web 应用程序中ASP.NET,单击对象时执行此操作。此操作由列表视图进程对象控制器的列表视图进程对象控制器指定。您可以使用自定义"简单操作"替换此操作。本主题演示如何执行此操作。
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
移动平台不支持本主题中描述的方法。如果需要在移动应用程序中实施此方案,请使用支持中心与我们联系
.
Tip 提示
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E3275
完整的示例项目可在 DevExpress 代码示例数据库中找到,http://www.devexpress.com/example=E3275
.
Set a Custom Action as Default
将自定义操作设置为"默认"
Assume you have the following AddressBookRecord persistent class.
假设您具有以下通讯簿记录持久性类。
[DefaultClassOptions, ImageName("BO_Contact")] public class AddressBookRecord : BaseObject { public AddressBookRecord(Session session) : base(session) { } private string name; public string Name { get { return name; } set { SetPropertyValue(nameof(Name), ref name, value); } } private string email; public string Email { get { return email; } set { SetPropertyValue(nameof(Email), ref email, value); } } private string phoneNumber; public string PhoneNumber { get { return phoneNumber; } set { SetPropertyValue(nameof(PhoneNumber), ref phoneNumber, value); } } }
Let us consider the WriteMailController View Controller that provides the WriteMail Action for AddressBookRecord objects. This Action invokes the program that is associated with the MailTo protocol on an end-user's computer.
让我们考虑为通讯簿记录对象提供 WriteMail 操作的 WriteMail 控制器。此操作调用与最终用户计算机上的 MailTo 协议关联的程序。
using System.Diagnostics; // ... public class WriteMailController : ViewController { private SimpleAction writeMailAction; public WriteMailController() { TargetObjectType = typeof(AddressBookRecord); writeMailAction = new SimpleAction(this, "WriteMail", PredefinedCategory.Edit); writeMailAction.ToolTip = "Write e-mail to the selected address book record"; writeMailAction.SelectionDependencyType = SelectionDependencyType.RequireSingleObject; writeMailAction.ImageName = "BO_Contact"; writeMailAction.Execute += writeMailAction_Execute; } void writeMailAction_Execute(object sender, SimpleActionExecuteEventArgs e) { AddressBookRecord record = (AddressBookRecord)e.CurrentObject; string startInfo = String.Format( "mailto:{0}?body=Hello, {1}!%0A%0A", record.Email, record.Name); Process.Start(startInfo); } }
By default, the Action specified by the ProcessCurrentObjectAction property of the ListViewProcessCurrentObjectController invokes a Detail View with the clicked object (the ListViewShowObject Action is specified by default). However, the ListViewProcessCurrentObjectController exposes the ListViewProcessCurrentObjectController.CustomProcessSelectedItem event, which you can handle to replace the default Action. The code below demonstrates how to handle this event in WriteMailController to execute a WriteMail Action instead of ListViewShowObject. Subscribe to the CustomProcessSelectedItem event in the overridden OnActivated method. In the event handler, execute the WriteMail Action by invoking the SimpleAction.DoExecute method.
默认情况下,由 ListViewProcessCurrentObject 控制器的 ProcessCurrentObjectAction 属性指定的操作使用单击的对象调用详细信息视图(默认情况下指定了 ListViewShowObject 操作)。但是,listViewProcessCurrentObject控制器公开列表视图进程对象控制器.自定义过程选择项目事件,您可以处理该事件来替换默认操作。下面的代码演示如何在 WriteMail 控制器中处理此事件以执行 WriteMail 操作,而不是 listViewShowObject。订阅重写的 On 已激活方法中的自定义进程选择项事件。在事件处理程序中,通过调用 SimpleAction.DoExecute 方法执行 WriteMail 操作。
using DevExpress.ExpressApp.SystemModule; // ... public class WriteMailController : ViewController { // ... private ListViewProcessCurrentObjectController processCurrentObjectController; protected override void OnActivated() { base.OnActivated(); processCurrentObjectController = Frame.GetController<ListViewProcessCurrentObjectController>(); if (processCurrentObjectController != null) { processCurrentObjectController.CustomProcessSelectedItem += processCurrentObjectController_CustomProcessSelectedItem; } } private void processCurrentObjectController_CustomProcessSelectedItem( object sender, CustomProcessListViewSelectedItemEventArgs e) { e.Handled = true; writeMailAction.DoExecute(); } protected override void OnDeactivated() { if (processCurrentObjectController != null) { processCurrentObjectController.CustomProcessSelectedItem -= processCurrentObjectController_CustomProcessSelectedItem; } base.OnDeactivated(); } }
The following image illustrates that the WriteMail action is executed when clicking a record in the AddressBookRecord objects' List View.
下图说明在"通讯簿记录"对象列表视图中单击记录时执行 WriteMail 操作。
You may notice that now there is no option to invoke a Detail View to edit a record. You can add to following Controller to fix this.
您可能会注意到,现在没有调用详细信息视图来编辑记录的选项。您可以添加到以下控制器以解决此问题。
using DevExpress.ExpressApp.SystemModule; // ... public class EditAddressBookRecordController : ViewController<ListView> { public EditAddressBookRecordController() { TargetObjectType = typeof(AddressBookRecord); SimpleAction editAddressBookRecordAction = new SimpleAction(this, "EditAddressBookRecord", PredefinedCategory.Edit); editAddressBookRecordAction.ImageName = "Action_Edit"; editAddressBookRecordAction.SelectionDependencyType = SelectionDependencyType.RequireSingleObject; editAddressBookRecordAction.Execute += editAddressBookRecordAction_Execute; } void editAddressBookRecordAction_Execute(object sender, SimpleActionExecuteEventArgs e) { ListViewProcessCurrentObjectController.ShowObject( e.CurrentObject, e.ShowViewParameters, Application, Frame, View); } }
Note 注意
Such a controller is required for Windows Forms applications only, as the WebModificationsController.EditAction Action is available in ASP.NET applications.
这种控制器仅对 Windows 窗体应用程序是必需的,因为 WebModifications 控制器 ASP.NET。
To leave an option to replace the WriteMail Action in another Controller, you can expose this action via a public property.
要保留一个选项,以替换其他控制器中的 WriteMail 操作,可以通过公共属性公开此操作。
public class WriteMailController : ViewController { // ... public SimpleAction DefaultListViewAction { get { return writeMailAction; } set { writeMailAction = value; } } }
Proceed to the next section of this topic to see how this property can be used.
继续本主题的下一部分,了解如何使用此属性。
Replace a Custom Default Action
替换自定义默认操作
Consider the following PhoneCallController View Controller than provides the PhoneCall Action. This action initiates dialing PhoneNumber of the current AddressBookRecord object in Skype.
请考虑以下电话呼叫控制器视图控制器比提供电话呼叫操作。此操作将启动在 Skype 中拨打当前通讯簿记录对象的电话机号。
public class PhoneCallController : ViewController { private SimpleAction phoneCallAction; public PhoneCallController() { TargetObjectType = typeof(AddressBookRecord); phoneCallAction = new SimpleAction(this, "PhoneCall", PredefinedCategory.Edit); phoneCallAction.ToolTip = "Call the current record via Skype"; phoneCallAction.ImageName = "BO_Phone"; phoneCallAction.SelectionDependencyType = SelectionDependencyType.RequireSingleObject; phoneCallAction.Execute += skypeCallAction_Execute; } void skypeCallAction_Execute(object sender, SimpleActionExecuteEventArgs e) { Process.Start("skype:" + ((AddressBookRecord)e.CurrentObject).PhoneNumber); } protected override void OnActivated() { base.OnActivated(); View.CurrentObjectChanged += View_CurrentObjectChanged; } void View_CurrentObjectChanged(object sender, EventArgs e) { phoneCallAction.Enabled.SetItemValue("PhoneIsSpecified", !String.IsNullOrEmpty(((AddressBookRecord)View.CurrentObject).PhoneNumber)); } }
The List View's default Action provided by the custom WriteMailController Controller can be replaced with another Action, if it is exposed via a public property. To replace the WriteMail action with PhoneCall, add the following code to the PhoneCallController class' OnActivated method.
如果通过公共属性公开列表视图的默认操作,则可以将其替换为其他操作。要将"写入邮件"操作替换为电话呼叫,请将以下代码添加到电话呼叫控制器类的 On 已激活方法。
protected override void OnActivated() { // ... WriteMailController writeMailController = Frame.GetController<WriteMailController>(); if (writeMailController != null) writeMailController.DefaultListViewAction = phoneCallAction; }
The following image illustrates that the PhoneCall action is executed when clicking a record in the AddressBookRecord objects' List View.
下图说明在单击"通讯簿记录"对象列表视图中的记录时执行"电话呼叫"操作。
Subscribing to the ListViewProcessCurrentObjectController's CustomProcessSelectedItem event, as demonstrated at the beginning of this topic is not a recommended, since there is the possibility that the PhoneCallController Controller will be activated after WriteMailController, and so the WriteMail Action will remain default.
不建议订阅 ListViewProcessCurrentTotoToToToToView 控制器的自定义过程选择项目事件,如本主题开头所示,因为电话呼叫控制器控制器可能会被激活在写入邮件控制器之后,因此"写入邮件操作"将保持默认值。
Note 注意
An example provided in this topic is Windows Forms specific just because Actions that start external programs use the Process.Start method. However, the main concept illustrated here in platform-independent - you can access the ListViewProcessCurrentObjectController Controller from ASP.NET-specific Controllers in the same manner.
本主题中提供的一个示例是特定于 Windows 窗体的,只是因为启动外部程序的操作使用 Process.Start 方法。但是,此处所示的主要概念与平台无关 - 您可以以相同的方式从ASP.NET特定控制器访问 listViewProcessCurrentObject 控制器。