How to: How to Replace a List View's Default Action: Replace the default list view button

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.

List view can be attached showing the operation of these functions are specific to the list view. In addition to these operations, each list view has an invisible default "simple." In Windows Forms application, press the ENTER key or double-click the selected object to perform this operation. ASP.NET, do this when you click an object in a Web application. This list consists of a list view Process object view Process object controller controller specified. You can use a custom "simple" with this operation. This topic demonstrates how to do this.

Note 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
Mobile platform approach described in this topic is not supported. If you need to implement this program in a mobile application, please contact us using Support Center

.

Tip Tip
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E3275
Complete sample project can be found in the code sample database DevExpress, http://www.devexpress.com/example=E3275

.

Set a Custom Action as Default

The custom action is set to "Default"

Assume you have the following AddressBookRecord persistent class.

Suppose you have the following address book records the 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.

Let us consider WriteMail operation for address book records the object WriteMail controller. MailTo protocol on the procedure call and end-user computer associated with this operation.

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.

By default, the click operation using the object specified by the ProcessCurrentObjectAction property ListViewProcessCurrentObject controller call details view (default specified ListViewShowObject operation). However, listViewProcessCurrentObject open a list view control process object controller. Custom process to select an item event, you can handle this event to replace the default action. The following code shows how to handle this event in WriteMail controller to perform WriteMail operation, rather than listViewShowObject. Subscribe to rewrite On activated custom processes methods of selection events. In the event handler, do WriteMail by calling SimpleAction.DoExecute method.

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.

FIG WriteMail the operation instructions in the "Address Book Record" click Record Object list view.

ReplaceDefaultActionWin

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.

You may notice that now the option to edit the recording details view is not called. You can add the following controllers to address this issue.

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

Note 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.

下图说明在单击"通讯簿记录"对象列表视图中的记录时执行"电话呼叫"操作。

ReplaceDefaultActionWin2

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.
An example provided in this topic is specific to Windows Forms, just because Process.Start start operation using the method of the external program. However, the main concept shown here has nothing to do with the platform - you can access listViewProcessCurrentObject controller from ASP.NET particular controller in the same way.

Guess you like

Origin www.cnblogs.com/foreachlife/p/How-to-Replace-a-List-View-s-Default-Action.html