How to: Implement a Property Editor Based on a Custom Control (WinForms) 如何:基于自定义控件(WinForms)实现属性编辑器
This topic explains how to implement a Property Editor for WinForms applications. For demo purposes, the integer Property Editor based on the NumericUpDown control is implemented in this example.
本主题介绍如何实现 WinForms 应用程序的属性编辑器。出于演示目的,在此示例中实现基于数字向上控件的整数属性编辑器。
Note 注意
You can see the code implemented here in the FeatureCenter Demo, installed with XAF. This demo is located in the %PUBLIC%\Documents\DevExpress Demos 19.2\Components\eXpressApp Framework\FeatureCenter folder, by default.
If you intend to use a DevExpress WinForms control that is not integrated to XAF by default, refer to the How to: Implement a Property Editor Using a DevExpress WinForms Control topic.
您可以在功能中心演示中看到此处实现的代码,该演示随 XAF 一起安装。此演示位于%PUBLIC%\Documents\DevExpress Demos 19.2\Components\eXpressApp Framework\FeatureCenter folder, by default.
如果打算使用默认情况下未集成到 XAF 的 DevExpress WinForms 控件,请参阅"如何:使用 DevExpress WinForms 控件实现属性编辑器"主题。
Follow these steps to implement a WinForms Property Editor.
1. In the WinForms module project, inherit the PropertyEditor or WinPropertyEditor class. Note that your editor should be public.
2. Apply the PropertyEditorAttribute to specify the data type for which the Property Editor will be used (Int32 in this example). If you pass true to the PropertyEditor attribute's last parameter, the Property Editor will be used for all integer properties in any business class.
3. Override the CreateControlCore method. In this method, instantiate, initialize and return the control instance (the NumericUpDowncontrol in this example).
4. Determine which event of the control occurs when the editing value is changed by a user (e.g., the NumericUpDown.ValueChangedevent). Refer to the control's documentation to find the appropriate event. Subscribe to this event and call the OnControlValueChanged method (which internally raises the PropertyEditor.ControlValueChanged event) from the event handler.
5. Override the Dispose method and unsubscribe the event handled in the previous step.
6. Optionally, support the IInplaceEditSupport interface and implement the IInplaceEditSupport.CreateRepositoryItem method. This step is only required if you are going to make the column editable in the editable List View.
按照以下步骤实现 WinForms 属性编辑器。
1. 在 WinForms 模块项目中,继承属性编辑器或 WinPropertyEditor 类。请注意,您的编辑器应该是公开的。
2. 应用属性编辑器属性以指定将使用属性编辑器的数据类型(本例中为 Int32)。如果将 true 传递给 PropertyEditor 属性的最后一个参数,则属性编辑器将用于任何 Business 类中的所有整数属性。
3. 覆盖创建控制核心方法。在此方法中,实例化、初始化和返回控件实例(本示例中的数值向下控件)。
4. 确定当用户更改编辑值时发生的控件事件(例如,数字上部.Valuechangedevent)。请参阅控件的文档以查找适当的事件。订阅此事件并从事件处理程序调用 OnControlValueChanged 方法(在内部引发属性编辑器.ControlValue 更改事件)。
5. 重写 Dispose 方法并取消订阅上一步中处理的事件。
6. 可选,支持 IinplaceEdit 支持接口并实现 IinplaceEditSupport.创建存储库项目方法。仅当要使列在可编辑列表视图中可编辑时,才需要此步骤。
The following code demonstrates CustomIntegerEditor class implementation based on the steps above.
以下代码演示了基于上述步骤的自定义IntegerEditor类实现。
using System.Windows.Forms; using DevExpress.ExpressApp.Model; using DevExpress.ExpressApp.Editors; using DevExpress.ExpressApp.Win.Editors; using DevExpress.XtraEditors.Repository; // ... [PropertyEditor(typeof(Int32), false)] public class CustomIntegerEditor : PropertyEditor, IInplaceEditSupport { private NumericUpDown control = null; protected override void ReadValueCore() { if(control != null) { if(CurrentObject != null) { control.ReadOnly = false; control.Value = (int)PropertyValue; } else { control.ReadOnly = true; control.Value = 0; } } } private void control_ValueChanged(object sender, EventArgs e) { if(!IsValueReading) { OnControlValueChanged(); WriteValueCore(); } } protected override object CreateControlCore() { control = new NumericUpDown(); control.Minimum = 0; control.Maximum = 5; control.ValueChanged += control_ValueChanged; return control; } protected override void OnControlCreated() { base.OnControlCreated(); ReadValue(); } public CustomIntegerEditor(Type objectType, IModelMemberViewItem info) : base(objectType, info) { } protected override void Dispose(bool disposing) { if(control != null) { control.ValueChanged -= control_ValueChanged; control = null; } base.Dispose(disposing); } RepositoryItem IInplaceEditSupport.CreateRepositoryItem() { RepositoryItemSpinEdit item = new RepositoryItemSpinEdit(); item.MinValue = 0; item.MaxValue = 5; item.Mask.EditMask = "0"; return item; } protected override object GetControlValueCore() { if(control != null) { return (int)control.Value; } return null; } }
To display a particular property using the CustomIntegerEditor Property Editor, customize the Application Model. Invoke the Model Editor for the WinForms module project and navigate to the required BOModel| Class| OwnMembers| Member node. Set the node's IModelCommonMemberViewItem.PropertyEditorType property to CustomIntegerEditor. After this, the property specified by the Member node will be displayed by the CustomIntegerEditor in all Views. To use a CustomIntegerEditor Property Editor in a specific Detail View only, use the PropertyEditorType property of the Views | <DetailView> | Items | <PropertyEditor> node instead.
要使用自定义IntegerEditor属性编辑器显示特定属性,请自定义应用程序模型。调用 WinForms 模块项目的模型编辑器并导航到所需的 BOModel*类*拥有会员*成员节点。将节点的 IModelCommonMemberViewItem.属性编辑器类型属性设置为自定义Integer编辑器。在此之后,成员节点指定的属性将由自定义Integer编辑器在所有视图中显示。要仅在特定详细信息视图中使用自定义IntegerEditor属性编辑器编辑器,请使用视图的属性编辑器类型属性 |<DetailView>*项目 |<PropertyEditor>节点。
Note 注意
You may need to implement the IAppearanceFormat interface and manually apply the IAppearanceFormat.BackColor, IAppearanceFormat.FontColor and IAppearanceFormat.FontStyle settings of the Conditional Appearance Module to the created control.
您可能需要实现 I外观格式接口,并手动将 I外观格式.BackColor.I外观格式.FontColor.FontColor 和 I外观格式.FontStyle 设置的条件外观模块的 FontStyle 设置应用于创建的控件。
XAF开发成品案例参考
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
作者博客: http://www.cnblogs.com/foreachlife
欢迎加入CIIP框架\XAF技术应用交流群: 336090194 群文件中有更多相关工具及文档资料
转载请注明出处。多谢!
欢迎加我微信: admiralcn 或扫码: