Custom Attribute Editor in xaf

Source: Internet
Author: User

What is xaf? If you have never heard of it, this article may not help you, but it is helpful if you are looking for a solution to the same problem. (Note: all examples are in the web project, and the WIN project works the same)

Xaf automatically generates the UI from the service class. Automatically generates the required Attribute Editor based on the type of each attribute of the business class. For example, for a string attribute in a business class, a text box is generated (as shown on the interface ). Generate a date selection box for datetime type attributes.

Common Attribute Editor classes are shown in the following table (under the WEB Project ):

tpye propertyeditortype remarks
string devexpress. expressapp. Web. Editors. aspx. aspxstringproertyeditor display text box
int32 devexpress. expressapp. Web. Editors. aspx. aspxintproertyeditor display the adjustable number input box
Boolean devexpress. expressapp. Web. Editors. aspx. aspxbooleanproertyeditor select button
datetime devexpress. expressapp. Web. Editors. aspx. aspxdatetimeproertyeditor time frame
Enum devexpress. expressapp. Web. Editors. aspx. aspxenumproertyeditor drop-down list
devexpress. expressapp. Web. Editors. aspx. aspxlookuppropertyeditor the drop-down box usually forms a one-to-many relationship with this object

When creating a business class, these objects are automatically associated with the properties of the business class and do not need to be declared. You can also select another property editor. You can find the xafml file on the application | views | items | propertyeditor node or application | views | items | propertyeditor node. For example:

    • Application | views | items | propertyeditor node corresponding to the rating attribute:
 
<Application> <views> <detailview id = "song_detailview"> <items> <propertyeditor propertyname = "rating" propertyeditortype = "mysolution. module. web. mysolutionaspnetmodule. webstarratingpropertyeditor "/> </items> </detailview> </views> </Application>
    • Application | bomodel | class | member node corresponding to the rating attribute:
<Application> <views> <detailview id = "song_detailview"> <items> <propertyeditor propertyname = "rating" propertyeditortype = "mysolution. module. web. mysolutionaspnetmodule. webstarratingpropertyeditor "/> </items> </detailview> </views> </Application>

Of course, the best practice is to use a visual model Editor, which can also be found under two nodes,

 

Different types are allowed for different attributes. Is the Attribute Editor that can be selected for the string type.

All attribute editors must be inherited by the propertyeditor class. The inheritance structure is as follows:

To implement a custom property editor, you must first determine which propertyeditor derived class to inherit from and the controls to use.

What I want to do now is: implement a one-to-many relationship drop-down attribute editing box. The drop-down box must have a ready-to-use tree structure. At first, I planned to inherit from aspxlookuppropertyeditor, which is the default Attribute Editor of the business type. However, this time it is a little troublesome. The aspxlookuppropertyeditor class contains aspxcombobox, And the aspxcombobox control cannot contain a tree. Because it is bound to an object rather than a simple value, the propertyeditor Derived classes in the list above are not suitable. This time I chose to inherit the aspxobjectpropertyeditorbase class. This is the base class of the Object Property editor. The aspxlookuppropertyeditor class is inherited by this class.

BecauseCodeThere are relatively few methods, but the focus is that you may need to find the appropriate one among many rewrite methods, so you don't need to draw class diagrams. Directly paste the Code: (when you look at the code, you can view the code by the number marked with the comment, which is the order in which I complete the code)

[Propertyeditor ()] // to display your custom editor in the model Editor, add this code. You can set parameters to indicate the Data Type of the editor. Public class aspxtreepropertyeditor: aspxobjectpropertyeditorbase {private weblookupeditorhelper helper; Public aspxtreepropertyeditor (type objecttype, imodelmemberviewitem model): Base (objecttype, model) {condition = true ;} // 1. After an excuse is integrated, you only need to implement this rewrite method. The purpose is to return a custom attribute Editor (that is, a control) protected override webcontrol createeditmodecontrolcore () {return createtreedropdown ();} treedropdown Createtreedropdown () {try {// 2. The specific control needs to use a drop-down control. I implemented this control in another class. Treedropdown = new treedropdown (); treedropdown. treelist. datasource = helper. createlistview (currentobject ). collectionsource. list; treedropdown. treelist. keyfieldname = "oid"; treedropdown. treelist. parentfieldname = "parent! Key "; treedropdown. treelist. clientinstancename = "treelist"; treedropdown. treelist. width = unit. parse ("100%"); treedropdown. clientinstancename = "dropdownedit"; treedropdown. treelist. previewfieldname = "name"; treelisttextcolumn Col = new treelisttextcolumn (); Col. name = "name"; Col. fieldname = "name"; treedropdown. treelist. columns. add (COL); treedropdown. readonly = true; treedropdown. treelist. setti Ngsbehavior. autoexpandallnodes = true; treedropdown. treelist. customcallback + = new treelistcustomcallbackeventhandler (treelist_customcallback); Return treedropdown;} catch (exception e) {Throw e ;}// 7. Because aspxtreelist has no node Click Time, so I used the client's Click Event and asked him to return a callback method. Void treelist_customcallback (Object sender, treelistmcmcallbackeventargs e) {// The attribute is assigned again when the user changes the selection. To save the object. (Treedropdown) Editor ). value = helper. getobjectbykey (currentobject, E. argument); editvaluechangedhandler (sender, eventargs. empty);} // 6. A helper object is used to create the public override void setup (objectspace, xafapplication application) {base. setup (objectspace, application); If (memberinfo. ispersistent) {helper = new weblookupeditorhelper (application, objectspace, memberinfo. memberty Peinfo, model) ;}else {helper = new weblookupnonpersistenteditorhelper (application, objectspace, memberinfo. membertypeinfo, model) ;}}// 3. Then I generated this class. Class treedropdown: devexpress. web. aspxeditors. aspxdropdownedit {aspxtreelist treelist; Public aspxtreelist treelist {get {return treelist ;}} public treedropdown () {treelist = new aspxtreelist (); // 4. In this class, I need to add a template containing the aspxtreelist control to it, so I need to implement an itemplate interface this. dropdownwindowtemplate = new treelisttemplate (treelist);} protected override void onprerender (eventargs e) {string script = @ "<SCRIPT type = 'text/JavaScript '> function rowclickhandler (S, E) {dropdownedit. setkeyvalue (E. nodekey); treelist. getnodevalues (E. nodekey, 'name', setdropdowntext); dropdownedit. hidedropdown (); treelist. performcallback (E. nodekey);} function setdropdowntext (value) {dropdownedit. settext (value)} </SCRIPT> "; this. registerscriptblock ("", script); treelist. clientsideevents. nodeclick = "rowclickhandler"; base. onprerender (E);} // 5. Implement the itemplate interface to complete a template Item class treelisttemplate: itemplate {aspxtreelist treelist; Public treelisttemplate (aspxtreelist treelist) {This. treelist = treelist;} public void instantiatein (control container) {container. controls. add (treelist );}}}

 

It can be seen that I did not write code in one breath, but from the top layer of the abstract, from the outside to the inside. You have to use xaf, and the framework forces you. The things completed after the meeting are as follows:

Finally, download the packaged source code (my version is xaf10.1.4 ). You should know that I have not provided a database. It is unnecessary for the xaf framework and will be automatically generated. You may need to input some data in the following interface for testing.

 

 

Note: In the original code of this article, the Chinese attribute name and the above code annotation are used, just for convenience of demonstration, and does not represent my programming style. In addition, xaf is a commercial control. All code demos use the officially downloaded evaluation version.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.