原文:
WPF笔记(1.9 样式和控件模板)——Hello,WPF!
<
Window
>
<
Window.Resources
>
<
Style x:Key
=
"
myStyle
"
TargetType
=
"
{x:Type TextBlock}
"
>
<
Setter Property
=
"
VerticalAlignment
"
Value
=
"
Center
"
/>
<
Setter Property
=
"
FontWeight
"
Value
=
"
Bold
"
/>
<
Setter Property
=
"
FontStyle
"
Value
=
"
Italic
"
/>
</
Style
>
</
Window.Resources
>
<
DockPanel
>
<
StackPanel
>
<
TextBlock Style
=
"
{StaticResource myStyle}
"
>
Name:
</
TextBlock
>
<
TextBox Text
=
"
{Binding Path=Name}
"
/>
<
TextBlock Style
=
"
{StaticResource myStyle}
"
>
Nick:
</
TextBlock
>
<
TextBox Text
=
"
{Binding Path=Nick}
"
/>
</
StackPanel
>

</
DockPanel
>
</
Window
>
代码很容易懂,记住用Setter定义每一个样式,注意指定了x:Key,然后哪个控件需要应用样式,就在控件里面指定Style;如果不指定x:Key,则所有控件都使用这个样式(当然也不会给控件设置Style = " {StaticResource myStyle} ")。
资源的另一个用途是样式设置:






















代码很容易懂,记住用Setter定义每一个样式,注意指定了x:Key,然后哪个控件需要应用样式,就在控件里面指定Style;如果不指定x:Key,则所有控件都使用这个样式(当然也不会给控件设置Style = " {StaticResource myStyle} ")。