1: Write in multiply line when writing xaml.
2: The Code should be segmented. A piece of code can be put in different files (SRP) for one purpose ).
3: Use the correct container: if there is a gap between the button and textbox side by side, do not use a grid with three columns. Use stackpanel and the padding and magging of the space itself.
4: x: Type is recommended after DataTemplate
<DataTemplate DataType = {x: Type string}> <TextBlock Text = {Binding}> </DataTemplate> // You are advised to add the DataType attribute to identify the Type of binding, self-help.
5: The ControlTemplate must have TargetType.
<ControlTemplate TargetType = {x: Type...}>... </ControlTemplate> // you must have ContentType.
6: All Fonts definitions are implemented at a high level, or even on the windows layer.
<Window> <UserControl> Fonts definition ...... </UserControl> </Window>
7. differentiate ContentPresanter and ContentControls
ContentControl is the base class for every control that displays "content" (example: Label), ContentPresenter is the code used internally by ContentControl to display the content-so: 1. contentPresenter is more lightweight 2. contentPresenter is designed to be used inside control templates 3. contnetPresenter is designed to be used as-is while ContentControl is designed to be extended (custom CT)
ContentControlIs a base class for controls that contain other elements and haveContent-Property (for example,Button).
ContentPresenterIs used inside control templates to display content.
ContentControl, When used directly (it's supposed to be used as a base class), has a control template that uses ContentPresenter to display it's content.
EDIT: My rules of thumb (not applicable in every case, use your judgment ):
- Inside
ControlTemplateUseContentPresenter
- Outside
ControlTemplate(IncludingDataTemplateAnd outside templates) try not to use any of them, if you need to, you must preferContentPresenter
- Subclass
ContentControlIf you are creating a custom "lookless" control that host content and you can't get the same result by changing an existing control's template (that shocould be extremely rare ).
ContentControl has three attributes (Content, Template, ContentTemplate), which can be designed for CT. CT can contain ContentPresenter.
Object CT DT
ContentPresenter has only two attributes (Content and ContentTemplate). It does not have the Template attribute and is used to display content within DT or CT.
Object DT
// From the class definition of contentpresenter, we can see that Class ContentPresenter {public Object Content {get; set;} public DataTemplate ContentTemplate {get; set ;}}
8: PFG2 dedicated secret, custom ContentProperty