1:写xaml的时候写在multiply line里。
2:代码要分块,一段代码实现一个目的,分别放在不同的文件里(SRP)。
3:使用正确的container:如果需要按钮和textbox并排之间有一个gap,就不要用3列的grid做,应该用stackpanel和空间自身的padding,magging。
4: DataTemplate后面最好有x:Type
<DataTemplate DataType = {x:Type string}> <TextBlock Text={Binding}> </DataTemplate> //这里最好加上DataType属性,来辨明binding的是什么类型,自己方便。
5: ControlTemplate一定要有TargetType
<ControlTemplate TargetType = {x:Type ...}> ..... </ControlTemplate> //一定要有ContentType
6:所有Fonts的定义都放在高层级做,甚至可以放在windows层。
<Window> <UserControl> Fonts定义...... </UserControl> </Window>
7:区分ContentPresanter 和 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(自定义CT)
ContentControl
is a base class for controls that contain other elements and have a Content
-property (for example, Button
).
ContentPresenter
is 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
ControlTemplate
useContentPresenter
- Outside of
ControlTemplate
(includingDataTemplate
and outside templates) try not to use any of them, if you need to, you must preferContentPresenter
- Subclass
ContentControl
if 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 should be extremely rare).
ContentControl里有三个属性(Content, Template, ContentTemplate),是可以设计其CT的,CT里可以包含ContentPresenter
Object CT DT
ContentPresenter里只有两个属性(Content, ContentTemplate),没有Template属性,使用在DT或者CT内部显示content。
Object DT
//这个从contentpresenter的class定义就可以看出 Class ContentPresenter { public Object Content{get; set;} public DataTemplate ContentTemplate{get; set;} }
8: PFG2独门秘笈,自定义ContentProperty