1. Create customcontrol
After selecting the control base class, the first thing is to create a "customcontrol" in your project. We will find that a * is automatically generated in the project *. CS (or *. VB or other) files and themesgeneric. XAML (if not, they are customcontrol's background code file (code behind) and control's default topic file, open themesgeneric. in XAML, you will find that a style is automatically generated, which is the default style of your control, just as the WPF built-in control also has its default style. at this time, our work is divided into two parts: one is in XXX. edit the control logic in the CS file, but in generic. write its UI in XAML.
2. How is the style in generic. XAML associated with our controls?
Open the XXX. CS file and you will find that in the static constructor, vs automatically overwrites the defaultstylekey value of the control:
The following is a reference clip:
Static customcontrol1 ()
{
Defaultstylekeyproperty. overridemetadata (typeof (customcontrol1), new frameworkpropertymetadata (typeof (customcontrol1 )));
} |
We know that defaultstylekeyproperty is the attribute of the frameworkelement and frameworkcontentelement classes used to indicate the default style key value of the control. A special feature of this attribute is that we cannot think about it with the idea of inheritance, for example, the default style key value of a button is style1, and the default style key value of its subclass mybutton is style2 (or no default style is specified). Although mybutton can be converted to a button class, however, we do not want the default style key value to be style1. therefore, WPF uses the defaultstylekey metadata method in the static Construction Method of the subclass control to specify the default style of the subclass control. in the above Code, we specify new frameworkpropertymetadata (typeof (customcontrol1) as its new metadata value. This value represents, in the resource dictionary, we will find a style with the key value typeof (customcontrol1) as the default style of the control. this style is defined in generic. in XAML:
The following is a reference clip:
<Style targettype = "">
<Setter property = "template">
<Setter. value>
<Controltemplate targettype = "">
<Border background = ""
Borderbrush = ""
Borderthickness = "">
</Border>
</Controltemplate>
</Setter. value>
</Setter>
</Style> |
This may be a question. The style in the above XAML does not specify the key value. The default style key value required by our controls is typeof (customcontrol1 ), and the elements in the resource dictionary must have keys? This is the basic knowledge of style. in WPF, there are two ways to specify a key for style: one is to specify a key explicitly, but specify targettype without specifying a key explicitly, WPF automatically sets its key to typeof (targettype ). if you have experience creating styles for controls in blend, you will notice that blend provides an "apply to all" option when creating a style, this is why the secret of your style can be "apply to all.
3. the name "Generic. XAML" is not accidental.
As described above, you may be impulsive to generic. the style code in XAML is cut out, pasted to any place that can be found by our control, and then generic. XAML deletes or changes it to a more elegant name. If you are lucky, this is feasible, because the control will look up the style from the bottom up (page, app, theme, but now you have made a potential mistake: You have not provided the default style for the control. the default style here is actually "the style used when the default topic is found for the control or not ". this involves theme related topics in WPF. If you are interested, refer to the msdn SDK.
4. Create Your control logic
This is inevitable. You can referUsercontrol.
5. Build the control UI
It is worth mentioning that I really admire those guys who are "swimming naked" in The XAML ocean of vs. However, I recommend Microsoft Expression blend to accomplish this arduous task. in addition, if you find that the WPF built-in controls are useful in blend, but the controls we have created are not like this, please note that your control logic may be designed incorrectly.
6. coupling between the control UI and the logic
This is a problem that is easy to ignore but very important. The reason why we use customcontrol instead of usercontrol is that we want our controls to be the same as those built in WPF, its UI can be easily customized by other users or changed in the future. that is to say, the visual tree cannot be entangled with the background logic, because the elements in the visual tree may be completely changed by your control users. for example, if your control has a button in the visual tree and you have processed some controls in the Click Event of the button, it is very likely that your control creation has failed, this button may be deleted when you redefine the control template.