VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之XAF-如何改变列表点击时的默认行为

 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中没这个问题,所以不用管。

 


如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。 作者博客: http://www.cnblogs.com/foreachlife 欢迎加入XAF技术交流群讨论 XAF技术应用
转载请注明出处。多谢! 

相关教程