EPR类企业管理系统

在我们现有系统基础上或全新开发,提供定制服务
为您的企业高效快速实施ERP,WMS,MES,CRM管理系统
全面管控物料仓库、销售业务、采购业务、仓库业务
生产过程、质量检验、组织架构、业务报表


定制
QQ:460-3528

开发
QQ群:3360-90194

源码
微信:136-3650-3721

如何:替换列表视图的默认操作

列表视图可以伴随有代表这些列表视图特定功能的动作。除了这些动作外,每个列表视图还具有一个不可见的默认简单动作。在Windows Forms应用程序中,当按ENTER键或双击所选对象时,将执行此操作。在ASP.NET Web应用程序中,单击对象时将执行此操作。该操作由ListViewProcessCurrentObjectControllerListViewProcessCurrentObjectController.ProcessCurrentObjectAction属性指定。您可以使用自定义的简单操作替换此操作。本主题演示了如何执行此操作。

注意

移动平台不支持本主题中描述的方法。如果有必要在您的Mobile应用程序中实现此方案,请使用支持中心与我们联系。

提示

可在http://www.devexpress.com/example=E3275的DevExpress代码示例数据库中找到完整的示例项目。

将自定义操作设置为默认值

假设您具有以下AddressBookRecord持久性类。

[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); }
    }
}

让我们考虑WriteMailController 视图控制器,它为AddressBookRecord对象提供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);
    }
}

默认情况下,由ListViewProcessCurrentObjectController的ProcessCurrentObjectAction属性指定的Action调用带有单击对象的详细信息视图(默认情况下指定ListViewShowObject Action)。但是,ListViewProcessCurrentObjectController公开了ListViewProcessCurrentObjectController.CustomProcessSelectedItem事件,您可以处理该事件以替换默认的Action。下面的代码演示了如何在WriteMailController中处理此事件以执行WriteMail Action而不是ListViewShowObject。订阅CustomProcessSelectedItem重写的OnActivated方法中的事件。在事件处理程序中,通过调用SimpleAction.DoExecute方法来执行WriteMail Action 。

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();
    }
}

下图说明在AddressBookRecord对象的列表视图中单击记录时执行WriteMail操作。

ReplaceDefaultActionWin

您可能会注意到,现在没有选项可以调用明细视图来编辑记录。您可以添加到以下Controller来解决此问题。

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);
    }
}

ReplaceDefaultActionWin1

注意

仅在Windows Forms应用程序中才需要这种控制器,因为WebModificationsController.EditAction操作在ASP.NET应用程序中可用。

要保留替换另一个Controller中的WriteMail操作的选项,可以通过公共属性公开此操作。

public class WriteMailController : ViewController {
    // ...
    public SimpleAction DefaultListViewAction {
        get { return writeMailAction; }
        set { writeMailAction = value; }
    }
}

继续本主题的下一部分,以了解如何使用此属性。

替换自定义默认操作

考虑以下提供了PhoneCall操作的PhoneCallController视图控制器。此操作将启动在Skype中拨打当前AddressBookRecord对象的PhoneNumber的电话。

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));
    }
}

如果自定义WriteMailController控制器提供的列表视图的默认操作是通过公共属性公开的,则可以将其替换为另一个操作。要更换WriteMail用行动PhoneCall,下面的代码添加到PhoneCallController类的OnActivated方法。

protected override void OnActivated() {
    // ...
    WriteMailController writeMailController = Frame.GetController<WriteMailController>();
    if (writeMailController != null)
        writeMailController.DefaultListViewAction = phoneCallAction;
}

下图说明了在AddressBookRecord对象的“列表视图”中单击记录时执行了PhoneCall操作。

ReplaceDefaultActionWin2

订阅到ListViewProcessCurrentObjectControllerCustomProcessSelectedItem事件,因为在这个主题开始表现出不是推荐,因为存在的可能性PhoneCallController控制器会后,将激活WriteMailController,所以WriteMail行动将保持默认。

注意

本主题提供的示例是特定于Windows窗体的,仅因为启动外部程序的操作使用Process.Start方法。但是,此处说明的主要概念与平台无关,您可以以相同的方式从ASP.NET特定的控制器访问ListViewProcessCurrentObjectController控制器。

转载保留此链接,注明出处