原文:
《Programming WPF》翻译 第5章 4.元素类型样式
<!-- no Key -->
< Style TargetType =" {x:Type Button} " >
< Setter Property ="FontSize" Value ="32" />
< Setter Property ="FontWeight" Value ="Bold" />
</ Style >
<!-- no Key -->
< Style TargetType =" {x:Type TextBlock} " >
< Setter Property ="FontSize" Value ="32" />
< Setter Property ="FontWeight" Value ="Thin" />
< Setter Property ="Foreground" Value ="White" />
< Setter Property ="HorizontalAlignment" Value ="Center" />
</ Style >
< Button Grid.Row ="0" Grid.Column ="0" x:ID ="cell00" />
< TextBlock Grid.Row ="5" Grid.ColumnSpan ="5" x:ID ="statusTextBlock" />
命名样式非常有用,当你得到一组属性并应用到特点的元素上。然而,如果你想要应用一个统一的样式到所有确定元素类型的实例,设置TargetType而不用一个Key,如示例5-16所示。
示例5-16
<!-- no Key -->
< Style TargetType =" {x:Type Button} " >
< Setter Property ="FontSize" Value ="32" />
< Setter Property ="FontWeight" Value ="Bold" />
</ Style >
<!-- no Key -->
< Style TargetType =" {x:Type TextBlock} " >
< Setter Property ="FontSize" Value ="32" />
< Setter Property ="FontWeight" Value ="Thin" />
< Setter Property ="Foreground" Value ="White" />
< Setter Property ="HorizontalAlignment" Value ="Center" />
</ Style >
< Button Grid.Row ="0" Grid.Column ="0" x:ID ="cell00" />
< TextBlock Grid.Row ="5" Grid.ColumnSpan ="5" x:ID ="statusTextBlock" />
在示例
5-16 所示,我们已经得到了两种样式,一种是带有TargetType 的Button ,没有key ;另一种是带有TargetType 的TextBlock ,没有key 。它们都以同样的方式工作;当创建一个Button 或TextBlock 的实例而不用现实地设置Style 属性,它使用的样式将目标类型匹配到控件的类型。我们的元素类型样式返回了我们的游戏如图5-4 所示。元素类型样式是便利的,无论何时你想要所有特定元素的实例共享一个外观,依赖于范围。例如,迄今,在顶级窗体中,我们已经在示例中为样式设置了范围,如示例5-17。
示例5-17
<!--
Window1.xaml
-->
< Window >
<!-- every Button or TextBlock in the Window is affected -->
< Window.Resources >
< Style TargetType =" {x:Type Button} " > </ Style >
< Style TargetType =" {x:Type TextBlock} " > </ Style >
</ Window.Resources >
</ Window >
< Window >
<!-- every Button or TextBlock in the Window is affected -->
< Window.Resources >
< Style TargetType =" {x:Type Button} " > </ Style >
< Style TargetType =" {x:Type TextBlock} " > </ Style >
</ Window.Resources >
</ Window >
尽管如此,我们可能想缩小元素类型样式的范围。在我们的示例中,这将工作良好将样式限定范围在
示例5-18
<!--
Window1.xaml
-->
< Window >
< Grid >
<!-- only Buttons or TextBlocks in the Grid are affected -->
< Grid.Resources >
< Style TargetType =" {x:Type Button} " > </ Style >
< Style TargetType =" {x:Type TextBlock} " > </ Style >
</ Grid.Resources >
</ Grid >
<!-- Buttons and TextBlocks outside the Grid are unaffected -->
</ Window >
< Window >
< Grid >
<!-- only Buttons or TextBlocks in the Grid are affected -->
< Grid.Resources >
< Style TargetType =" {x:Type Button} " > </ Style >
< Style TargetType =" {x:Type TextBlock} " > </ Style >
</ Grid.Resources >
</ Grid >
<!-- Buttons and TextBlocks outside the Grid are unaffected -->
</ Window >
或者,如果你想使你的样式在你的工程中有更大的作用区域,你可以将它们放在应用程序范围内,如示例
5-19 。示例5-19
<!--
MyApp.xaml
-->
< Application >
<!-- every Button or TextBlock in the Application is affected -->
< Application.Resources >
< Style TargetType =" {x:Type Button} " > </ Style >
< Style TargetType =" {x:Type TextBlock} " > </ Style >
</ Application.Resources >
</ Application >
< Application >
<!-- every Button or TextBlock in the Application is affected -->
< Application.Resources >
< Style TargetType =" {x:Type Button} " > </ Style >
< Style TargetType =" {x:Type TextBlock} " > </ Style >
</ Application.Resources >
</ Application >
一般而言,理解元素类型的样式范围规则是有用的,因此你可以判断它们在各种
命名样式和元素类型样式
当对命名样式还是元素类型样式使用作出选择时,我们的一位评论家说,按照他的经验,一旦你有10个以上给予元素类型的样式,对一个特定的控件获取它的样式保持跟踪将非常困难。这是一个原因是我成为命名样式的粉丝。
对于我而言,样式是一个在一个地方应用到内容的语义标签,并且在另一个地方也能获得一个可视化表示。正如我们的TTT示例那样简单,我们已经得到了两个样式,一个是为了状态文字,另一个是为了移动的单元;在我们这么做之前,我们将要得到更多。主要的区别因素是我们在这些元素中显示的数据种类,而不是保持数据的元素类型。实际上,我们有一些分配到TextBox控件的样式,这将无论如何打消基于类型的样式,甚至是这个简单的应用程序。