How to: Implement a View Item 如何:实现视图项

This topic demonstrates how to implement a custom View Item and display it on all Detail Views. This View Item shows an image that corresponds to a specific business object type. You can specify this image in the Application Model. In this topic, we use a Windows control to display this View Item, so this example applies to WinForms applications only.

本主题演示如何实现自定义视图项并将其显示在所有详细信息视图上。此视图项显示对应于特定业务对象类型的图像。您可以在应用程序模型中指定此图像。在本主题中,我们使用 Windows 控件来显示此视图项,因此此示例仅适用于 WinForms 应用程序。

IconDetailItem

Tip 提示
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E242
完整的示例项目可在 DevExpress 代码示例数据库中找到,http://www.devexpress.com/example=E242

.

A custom View Item's implementation process consists of two parts:

  • Create a View Item
  • Add a Custom View Item to a Detail View

自定义视图项的实现过程由两部分组成:

  • 创建视图项
  • 将自定义视图项添加到详细视图

Create a View Item创建视图项

1. In the WinForms module project, create a ViewItem class descendant and name it "ClassIconDetailItem".

2. Override the CreateControlCore method to create a control that represents the View Item in a UI.

3. Create a custom interface that implements the IModelViewItem and name it "IModelClassIcon".

4. In this interface, define properties that coincide with View Item's properties you want to use in the Application Model. Do not define any properties in this interface if you do not want to specify a View Item's properties in the Application Model.

5. Pass this interface to the View Item's constructor as the the model parameter. This allows to initialize and configure the View Item using the data from the corresponding Application Model's node.

6. Decorate the ClassIconDetailItem class with the ViewItemAttribute and pass the IModelClassIcon interface as an attribute's parameter. This displays a new child node in the Application Model's ViewItems node after the solution rebuilds.

创建视图项

1. In WinForms 模块项目,创建 ViewItem 类后代并将其命名为"ClassIcon 详细信息项目"。

2. 覆盖"创建控制核心"方法以创建表示 UI 中的视图项的控件。

3. 创建一个自定义界面,实现IModelViewItem并将其命名为"IModelClassIcon"。

4. In此接口,请定义与要在应用程序模型中使用的视图项的属性一致的属性。如果不想在应用程序模型中指定视图项的属性,请不要在此接口中定义任何属性。

5. 将此接口作为模型参数传递给 View Item 的构造函数。这允许使用相应应用程序模型节点中的数据初始化和配置视图项。

6. 使用 ViewItem 属性修饰 ClassIcondetailItem 类,并将 IModelClassIcon 接口作为属性的参数传递。这将在解决方案重建后在应用程序模型的 ViewItems 节点中显示一个新的子节点。

RegisteredClassIcon

The following code demonstrates the custom View Item:

以下代码演示了自定义视图项:

using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Utils;
//...
public interface IModelClassIcon : IModelViewItem { }

[ViewItemAttribute(typeof(IModelClassIcon))]
public class ClassIconDetailItem : ViewItem {
    private IModelClassIcon Info;
    public ClassIconDetailItem(IModelClassIcon info, Type classType)
        : base(classType, info.Id) {
        Info = info;
    }
    protected override object CreateControlCore() {
        PictureBox imageControl = new PictureBox();
        string imageName = ((IModelDetailView)Info.Parent.Parent).ImageName;
        ImageInfo imageInfo = ImageLoader.Instance.GetLargeImageInfo(imageName);
        if(imageInfo.IsEmpty) {
            imageControl.Visible = false;
        }
        else {
            Image image = imageInfo.Image;
            imageControl.Image = image;
            imageControl.Width = image.Width;
            imageControl.Height = image.Height;
        }
        return imageControl;
    }
}

Add the Custom View Item on a Detail View

1. In the WinForms module project, implement the ModelNodesGeneratorUpdater<T> descendants (Generator Updaters) for the ModelDetailViewLayoutNodesGenerator and ModelDetailViewItemsNodesGenerator Node Generators. Name them "MyDetailViewLayoutUpdater" and "MyDetailViewItemUpdater".

2. Register these Updaters in the WinForms module class descendant's overridden ModuleBase.AddGeneratorUpdaters method.

在详细内容视图上添加自定义视图项

1. 在WinForms模块项目中,实现模型节点发电机更新器<T>后代(发电机更新器)的模型详细信息视图组节点发电机和模型详细信息查看项目发电机发电机。将它们命名为"My 详细信息视图布局更新器"和"My 详细信息查看项目更新程序"。

2. 将这些更新程序注册在 WinForms 模块类后代的重写模块Base.AddgeneratorUpdaters 方法中。

The following code shows how to do this:

以下代码演示如何执行此操作:

using System.ComponentModel;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Model.NodeGenerators;
using DevExpress.ExpressApp.Model.Core;
using DevExpress.ExpressApp.Layout;
//...
public class MyDetailViewLayoutUpdater : ModelNodesGeneratorUpdater<ModelDetailViewLayoutNodesGenerator> {
    public override void UpdateNode(ModelNode node) {
        IModelViewLayout layoutNode = (IModelViewLayout)node;
        IModelLayoutGroup mainGroup = 
            layoutNode.GetNode(ModelDetailViewLayoutNodesGenerator.MainLayoutGroupName) as IModelLayoutGroup;
        mainGroup.Direction = FlowDirection.Horizontal;
        IModelLayoutViewItem myItem = mainGroup.AddNode<IModelLayoutViewItem>("Icon");
        myItem.Index = int.MinValue;
        myItem.MaxSize = new System.Drawing.Size(64, 64);
        myItem.SizeConstraintsType = XafSizeConstraintsType.Custom;
        myItem.ViewItem = ((IModelCompositeView)layoutNode.Parent).Items.GetNode("Icon") as IModelViewItem;
    }
}

public class MyDetailViewItemUpdater : ModelNodesGeneratorUpdater<ModelDetailViewItemsNodesGenerator> {
    public override void UpdateNode(ModelNode node) {
        IModelViewItems itemsNode = (IModelViewItems)node;
        itemsNode.AddNode<IModelClassIcon>("Icon");
    }
}

[ToolboxItemFilter("Xaf.Platform.Win")]
public sealed partial class MySolutionWindowsFormsModule : ModuleBase {
    // ...
    public override void AddGeneratorUpdaters(ModelNodesGeneratorUpdaters updaters) {
        base.AddGeneratorUpdaters(updaters);
        updaters.Add(new MyDetailViewLayoutUpdater());
        updaters.Add(new MyDetailViewItemUpdater());
    }
}

Run the application and ensure that all the Detail Views contain class icons.

运行应用程序并确保所有详细信息视图都包含类图标。

原文地址:https://www.cnblogs.com/foreachlife/p/How-to-Implement-a-View-Item.html