HOWTO:XAF中如何自定义导出动作.

英文原文地址:http://documentation.devexpress.com/#Xaf/CustomDocument3287

The default List Editor in Windows Forms XAF applications is the XtraGrid control. This control has an option to export its content to various formats. In XAF applications, the export functionality is provided via the Export Action. This Action is presented by the abstract ExportController View Controller, which is the base for the XtraGridExportController Controller. The XtraGridExportController Controller is activated for all List Views. Its Export action is active when the current List Editor control is XtraGrid.

XAF程序中默认的列表编辑是XtraGrid,XtraGrid有一个可以导出多种文件格式的选项,在XAF程序中,导出功能是通过 Export 动作来实现,此动作由继承于VewController的ExportController来提供,EportController也是XtraGridExportController的父类,XtraGridExportGrid在所有的列表中均有效, 当列表编辑器是XtraGrid时.Export动作是可用的.

 

 

This topic describes how to access the XtraGridExportController and customize the Export Action behavior via the controller's events:

本文详细指出如何取得XtraGridExportController ,并如何通过控制器的如下事件自定义Export 动作的相关行为.

ExportController.CustomExport
ExportController.Exported

Customizing the Export Action via the CustomExport Event
通过 CustomExport 事件自定义 Export  动作.

The CustomExport event occurs before export, after a user specifies the target file name in the Save as dialog. The handler's FileName and Stream parameters provide you with access to the target file name and System.IO.Stream object, representing data to be exported. You can handle this event, to adjust objects being exported or to manually perform the export operation (set the handler's Handled parameter to true, in this case). In this example, we will handle this event to notify an end-user when the exporting List View has collapsed groups, and provide him with the ability to choose whether to expand them in the exported file. This will override the default behavior when the collapsed groups are always expanded in the exported file.

事件:CustomExport 发生在导出动作之前,用户指定另存为对话框中的文件名后.通过参数:FileNameStream 可以取得导出对象的文件名和表示导出数据的流对象:System.IO.Stream .通过调用本事件,你可以判断对象在导出后是否存在做一些导出选项的手工动作(在本实例中.设置handler 的 Handled 参数为True).在这个示例中,我们通过调用本事件,在最终用户收缩起列表导出的时候,发出提醒,并让最终用户选择是否导出收缩的表格.这会覆盖默认的行为.

To detect if there are collapsed groups, we need to iterate through the grid's group rows. The group rows have negative handles in the Grid View (see Process Group Rows). The expansion status of a group row can be determined by the GridView.GetRowExpanded method. We will display the message box when the collapsed group is detected, and set the GridOptionsPrint.ExpandAllGroups property, according to the user's selection. The following View Controller implements the required behavior:

判断列表是否收缩起列表,通过表格的GROUP ROWS.......

 

代码
using System.Windows.Forms;
using DevExpress.ExpressApp.Win;
using DevExpress.ExpressApp.Win.Editors;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.Win.SystemModule;
// ... 
public partial class CustomizeXtraGridExportController : ViewController
{
    
public CustomizeXtraGridExportController() {
        InitializeComponent();
        RegisterActions(components);
    }
    
private XtraGridExportController exportController;
    
protected override void OnActivated() {
        
base.OnActivated();
        exportController 
= Frame.GetController<XtraGridExportController>();
        exportController.CustomExport 
+= exportController_CustomExport;
    }
    
void exportController_CustomExport(object sender, CustomExportEventArgs e) {
        GridListEditor gridListEditor 
= 
            ((DevExpress.ExpressApp.ListView)View).Editor 
as GridListEditor;
        XafGridView gridView 
= gridListEditor.GridView;
        
if (HasCollapsedGroups(gridView)) {
            
if (WinApplication.Messaging.GetUserChoice(
                
"There are collapsed groups in the grid. Expand all groups in the exported file?",
                
"Export", MessageBoxButtons.YesNo) == DialogResult.Yes)
                gridView.OptionsPrint.ExpandAllGroups 
= true;
            
else gridView.OptionsPrint.ExpandAllGroups = false;
        }
    }
    
private bool HasCollapsedGroups(XafGridView gridView) {
        
if (gridView.GroupCount > 0) {
            
int rowHandle = -1;
            
while (gridView.IsValidRowHandle(rowHandle)) {
                
if (!gridView.GetRowExpanded(rowHandle)) return true;
                rowHandle
--;
            }
        }
        
return false;
    }
    
protected override void OnDeactivating() {
        
base.OnDeactivating();
        exportController.CustomExport 
-= exportController_CustomExport;
    }
}

 <注>以上代码通过在PROJECT.MODULE.WIN中新增一个:ViewController类.

 

End-users will see the following message box when exporting a List View containing collapsed groups:

 

最终用户在收缩列表导出时会看到下列对话框.
 

If the choice is "Yes", all the grid's rows will be exported.

如果选择"是",则表格行会被导出.

 

If the choice is "No", the rows belonging to the collapsed groups won't be included in exported file.

如果选择则"否",则被收缩的行不会在导出的文件中.

 
 
Customizing the Export Action via the Exported Event
通过Exported 事件自定义导出动作
 

The Exported event occurs after an export operation has been completed. The handler's FileName and Stream parameters provide you with access to the target file name and System.IO.Stream object, representing export data. You can handle this event to perform custom post-export actions. In this example, we will provide the end-user with the ability to open the exported file immediately. The following View Controller implements the required behavior.

 
Exported 事件产生在导出操作完成之后.参数FileNameStream 可以取得导出文件的名称和文件流对象(System.IO.Stream).你可以在导出动作完成后加入你想要的动作.
 
 

 

代码
using System.IO;
using System.Windows.Forms;
using DevExpress.ExpressApp.Win;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.Win.SystemModule;
// ... 
public partial class CustomizeXtraGridExportController : ViewController
{
    
public CustomizeXtraGridExportController() {
        InitializeComponent();
        RegisterActions(components);
    }
    
private XtraGridExportController exportController;
    
protected override void OnActivated() {
        
base.OnActivated();
        exportController 
= Frame.GetController<XtraGridExportController>();
        exportController.Exported 
+= exportController_Exported;
    }
    
void exportController_Exported(object sender, CustomExportEventArgs e) {
        
if (File.Exists(e.FileName)) {
            
if (WinApplication.Messaging.GetUserChoice("Open exported file?",
                
"Export", MessageBoxButtons.YesNo) == DialogResult.Yes)
                Process.Start(e.FileName);
        }
    }
    
protected override void OnDeactivating()
    {
        
base.OnDeactivating();
        exportController.Exported 
-= exportController_Exported;
    }
}

 

 

The following image illustrates the message box shown after the export has been finished.

导出完成后将显示下列的对话框.

 

posted @ 2010-11-05 16:05  方子  阅读(597)  评论(0编辑  收藏  举报