Back to the tutorial directory
Note: This articleArticleThe minimum requirement is 2.1 for the SPA project.
In the spa Engineering 2 tutorial (Part 1): custom controls, I introduced how to create custom controls through the iControl interface and spacontrolattribute feature type in the spa project. This method is to create a custom control from scratch. In this section of the tutorial, I will introduce another method to create a custom control, you can use the override inheritance control to create a custom control. As you can see from the name, this method does not need to create custom controls from scratch, but inherits the existing control definitions, you can rewrite the corresponding attributes and methods to meet your custom requirements.
We can use this method to create a multi-row text box control for the String Array (string []) type. The effect is as follows:
(Currently, the text box control in the spa project only supports the string type (instead of the string []). As for the multiline style, you can always set the style of the corresponding control by setting the stylekey or errorstylekey attribute of spacontrolattribute)
Now, we start to use the predefined textboxstring control to create this custom control named textboxlines.
The first step is to inherit the textboxstring control (all the pre-defined controls of the SPA project are executed in the mgen. spa. Controls. imple namespace)
// + Using mgen. spa. Controls. imple;
Public Class Textboxlines:Textboxstring
Next, we will rewrite the supported types of controls. In the spa Engineering 2 tutorial (Part 1): In custom controls, I have talked about iControl and the underlying istorage interface to define supported data types, the controlbase class of the control provides modifiable support for defining type attributes. Therefore, the second part is to directly rewrite the targettype so that it returns string [],Code:
Public Override TypeTargettype
{
Get
{
Return Typeof(String[]);
}
}
Then we need to set the properties of the internal control in the constructor of the inheritance type, because the textbox we need must support multiple rows, and the Textbox Control in textboxstring by default is only a single row input state. (Of course, we can also directly use the stylekey in the Control Feature Definition. However, if possible, the stylekey attribute should be left to the user to customize the style, and it is best to directly set the properties of the control in the constructor when customizing the control ).
In addition, after the spa Project 2.1, all predefined control types have been modified to better support the definition of inherited controls. For example, you can access the content control object of the selector control through the internalselector attribute. In this example, we need to use the internaltextbox attribute of the textboxstring control type.
Therefore, the constructor of the custom control textboxlines is as follows:
PublicTextboxlines ()
{
// Use textboxstring. internaltextbox to obtain the underlying Textbox Control.
// Set specific attributes so that textbox supports multi-line input.
Internaltextbox.Height= 100;
Internaltextbox.Acceptsreturn= True;
Internaltextbox.Textwrapping=System.Windows.Textwrapping.Wrap;
}
In this case, the control supports multi-line input and the internal support type is changed to string [], which is the last step: rewrite the data writing and reading methods. Similarly, we rewrite the controlbase method of the control base class. These methods are defined in the iControl interface and its istorage interface. These methods are already in the spa Engineering 2 tutorial (Part 1 ): custom Controls have been discussed.
In this example, we need to rewrite the getdata and setdata methods to ultimately complete the conversion logic of string [] (data storage type) and string (Data Type of the Textbox Control.
// Rewrite the getdata method: return the text of textbox to string [].
Public Override ObjectGetdata ()
{
If(String.Isnullorempty (internaltextbox.Text ))
Return Null;
ReturnInternaltextbox.Text.Split ((Char[])Null,Stringsplitoptions.Removeemptyentries );
}
// Rewrite the setdata method: Set string [] to textbox.
Protected Override VoidSetdataoverride (ObjectOBJ)
{
If(OBJ= Null)
Internaltextbox.Text= String.Empty;
Else
Internaltextbox.Text= String.Join (Environment.Newline ,(String[]) OBJ );
}
OK. The control execution is complete. Do not forget to define the control property type. You only need to inherit the spacontrolattride type, rewrite the getcontroloverride method, and return the corresponding control for execution.
(Note that the name of the control property type is usually [control execution type name] + controlattribute)
// + Using mgen. spa. controls;
// + Using mgen. spa. Data;
Public Class Textboxlinescontrolattribute:Spacontrolattribute
{
Protected Override IControlGetcontroloverride ()
{
Return New Textboxlines();
}
}
Finally, we can use our custom controls in the spa project data type:
Class Spaclass
{
[Spaheader("Item1")]
[Textboxlinescontrol]
Public String[] Lines {Get;Private Set;}
}
Source codeDownload
Download Page
Note: the link is the Microsoft SkyDrive page. When downloading, use a browser to download it directly. Some download tools may not be available for downloading.
Source code environment: Microsoft Visual Studio express 2012 for Windows Desktop
Note: The Source Code does not include the referenced external class library file: mgen SPA project