How to: Create and customize a custom GridLookUpEdit control

本文介绍如何创建并自定义GridLookUpEdit控件,通过覆盖OnLoaded方法初始化控件属性,包括数据源、列设置等,适用于需要在多个地方使用且保持一致性的编辑器场景。

How to: Create and customize a custom GridLookUpEdit control

  • Nov 13, 2018
  • 4 minutes to read

Assume that a GridLookUpEdit control needs to be used in multiple places, and all editors must display data from one predefined data source and contain predefined columns. In this instance, you can create a custom GridLookUpEdit control.

This example shows how to create a custom GridLookUpEdit control, and initialize its properties as required. The structure of descendant classes is based on the code template shown in the Custom Editors.Editors Structure topic.

To customize the control's settings, the OnLoaded method is overridden. In this method, the control's DataSource, columns and some other options are initialized. Note that columns created in this method are not accessible at design time.

using System.Drawing;
using System.Reflection;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using System.Data;


namespace DevExpress.MyGridLookUpEditors {

    //The attribute that points to the registration method
    [UserRepositoryItem("RegisterMyGridLookUpEdit")]
    public class RepositoryItemMyGridLookUpEdit : RepositoryItemGridLookUpEdit {

        //The static constructor which calls the registration method
        static RepositoryItemMyGridLookUpEdit() { RegisterMyGridLookUpEdit(); }

        //Initialize new properties
        public RepositoryItemMyGridLookUpEdit() {
        }

        protected override void OnLoaded() {
            base.OnLoaded();
            if (IsDesignMode) return;
            // Create two columns
            GridColumn colId = new GridColumn();
            colId.FieldName = colId.Caption = "ID";
            colId.VisibleIndex = 0;
            GridColumn colName = new GridColumn();
            colName.FieldName = colName.Caption = "Name";
            colName.VisibleIndex = 1;
            GridView gView = this.View;
            gView.Columns.Clear();
            gView.Columns.Add(colId);
            gView.Columns.Add(colName);
            // Hide the group panel
            gView.OptionsView.ShowGroupPanel = true;
            // Initialize data source
            DisplayMember = "Name";
            ValueMember = "ID";
            DataSource = GetData();
        }

        private DataTable GetData() {
            DataTable table = new DataTable();
            table.Columns.Add(new DataColumn("ID", typeof(int)));
            table.Columns.Add(new DataColumn("Name", typeof(string)));
            table.Rows.Add(new object[] { 0, "A" });
            table.Rows.Add(new object[] { 1, "B" });
            table.Rows.Add(new object[] { 2, "C" });
            return table;
        }


        //The unique name for the custom editor
        public const string MyGridLookUpEditName = "MyGridLookUpEdit";

        //Return the unique name
        public override string EditorTypeName { get { return MyGridLookUpEditName; } }

        //Register the editor
        public static void RegisterMyGridLookUpEdit() {
            //Icon representing the editor within a container editor's Designer
            Image img = null;

            EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(MyGridLookUpEditName,
              typeof(MyGridLookUpEdit), typeof(RepositoryItemMyGridLookUpEdit),
              typeof(GridLookUpEditBaseViewInfo), new ButtonEditPainter(), true, img));
        }



        //Override the Assign method
        public override void Assign(RepositoryItem item) {
            BeginUpdate();
            try {
                base.Assign(item);
                RepositoryItemMyGridLookUpEdit source = 
                    item as RepositoryItemMyGridLookUpEdit;
                if (source == null) return;
            }
            finally {
                EndUpdate();
            }
        }
    }


    public class MyGridLookUpEdit : GridLookUpEdit {

        //The static constructor which calls the registration method
        static MyGridLookUpEdit() { 
            RepositoryItemMyGridLookUpEdit.RegisterMyGridLookUpEdit(); }

        //Initialize the new instance
        public MyGridLookUpEdit() {
            //...
        }

        //Return the unique name
        public override string EditorTypeName { get { return 
            RepositoryItemMyGridLookUpEdit.MyGridLookUpEditName; } }

        //Override the Properties property
        //Simply type-cast the object to the custom repository item type
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemMyGridLookUpEdit Properties {
            get { return base.Properties as RepositoryItemMyGridLookUpEdit; }
        }

    }
}
Qt Quick enables you to build UIs around the behavior of components and how they connect with one another. You create components using Qt Quick and QML types that are available in the Design mode. You can specify values for the properties of a component to change its appearance and behavior. All QML types have a set of predefined properties, some of which control things that are visible to users, while others are used behind the scene. While it is useful to learn the basics of Qt Quick, you can also rely on Qt Design Studio to write the code for you when you drag-and-drop the ready-made components to the working area and change them to your liking by modifying their properties in the Design mode. You can always check up details in the extensive Qt Quick documentation by pressing F1. Creating Qt Quick Projects You can use wizards to create Qt Quick projects. Editing QML Files in Design Mode You can use the Form Editor or the Text Editor in the Design mode to develop Qt Quick applications. Creating Components In addition to your imported artwork, you can use the Design mode to customize ready-made components or design any custom form and shape directly as QML types. You can import visual assets in various formats, such as PNG, JPG, and SVG for use in the components. Managing Item Hierarchy You can manage the items in the current QML file and their relationships in the Navigator. Specifying Item Properties You can specify values for the properties of a component to change its appearance and behavior. All QML types have a set of predefined properties. Some properties, such as position, size, and visibility, are common to all QML types, whereas others are specific to the QML type. You can specify properties for your components in the Properties pane. Creating Animations You can use a timeline and keyframe based editor in the Timeline view to animate the properties of UI components. Animating properties enables their values to move through intermediate values at specified keyframes instead of immediately changing to the target value. Adding Connections You can create connections between the UI components and the application to enable them to communicate with each other. For example, how does the appearance of a button change on a mouse click and which action does the application need to perform in response to it. You can also create connections between UI components by binding their properties together. This way, when the value of a property changes in a parent component, it can be automatically changed in all the child components, for example. Adding States Qt Quick allows you to declare various UI states that describe how component properties change from a base state. Therefore, states can be a useful way of organizing your UI logic. You can associate transitions with items to define how their properties will animate when they change due to a state change. Related Topics Editing PathView Properties You can use a graphical spline editor to specify PathView paths. A path view lays out data provided by data models on a Path. Browsing ISO 7000 Icons You can add ISO 7000 icons from a library delivered with Qt Creator to UIs and change their color. Qt Quick UI Forms Some of the wizards create Qt Quick projects that contain UI forms (.ui.qml files). The forms use a purely declarative subset of the QML language and you can edit them in the Design mode. Using QML Modules with Plugins QML modules may use plugins to expose components defined in C++ to QML applications. Qt Creator cannot load the plugins to determine the details of the contained components, and therefore, the modules must provide extra type information for code completion and the semantic checks to work correctly. Converting UI Projects to Applications Qt Quick UI projects (.qmlproject) are useful for creating user interfaces. To use them for application development, you have to convert them to Qt Quick Application projects that contain .pro, .cpp, and .qrc files. Designing User Interfaces ◦ Creating Qt Quick Projects
最新发布
09-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值