在 Windows 窗体应用程序中,按下回车或双击列表会打开默认的详细视图。
在 ASP.NET Web 应用程序中,单击对象时执行此操作。
这一行为是由 ListViewProcessCurrentObjectController 的 ListViewProcessCurrentObjectController.ProcessCurrentObjectAction 按钮所定义的。
可以将此操作替换为自定义的简单操作。下面演示如何执行此操作。
示例代码在这里。
有下面的BO定义,地址记录。
[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("Name", ref name, value); } } private string email; public string Email { get { return email; } set { SetPropertyValue("Email", ref email, value); } } private string phoneNumber; public string PhoneNumber { get { return phoneNumber; } set { SetPropertyValue("PhoneNumber", ref phoneNumber, value); } } }
现在并为AddressBookRecord 对象操作加一个WriteMailController,并填加 WriteMail Action。这个操作将调用与最终用户的计算机上的 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); } }
默认情况下,只有选中了列表中的一条记录后,再点击上面我们填加的按钮,才会执行调用邮件客户端的动作。
下面,我们演示,在win中双击列表,即执行上面的动作。
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(); } }
下面是效果:
这时你会发现,你没办法修改记录了,即详细视图没办法显示出来了。下面代码来解决这样的问题,即,加个控制器,实现这些功能。
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); } }
注意:在web中没这个问题,所以不用管。