<Style TargetType="TextBlock"> <Setter Property="FontSize" Value="28"/> </Style> <Style TargetType="{x:Type TextBlock}"> <Setter Property="FontSize" Value="28"/> </Style>
完全沒區別~~前者用了默認的隱式的方式,后者用了一個顯式的命名空間引用。但其實是一樣的,都是自動給TextBlock應用樣式
<Style x:Key="{x:Type TextBlock}" TargetType="TextBlock"> <Setter Property="FontSize" Value="28"/> </Style>
這種寫法和上面的也完全沒有區別。將 TargetType 屬性設置為 TextBlock 而不為樣式分配 x:Key,樣式就會應用於所有 TextBlock 元素。這種情況下,x:Key 隱式設置為 {x:Type TextBlock}。
<Style x:Key="cc" TargetType="{x:Type TextBlock}"> <Setter Property="FontSize" Value="28"/> </Style>
如果將 x:Key 值顯式設置為 {x:Type TextBlock} 之外的任何值,如上面設置為 x:key="cc",Style 就不會自動應用於所有 TextBlock 元素。此時,必須通過使用 x:Key 值,將樣式顯式應用於 TextBlock 元素。
<Style x:Key="aa"> <Setter Property="Control.FontSize" Value="28"/> </Style>
樣式位於資源部分,並且未設置樣式的 TargetType 屬性,則必須提供 x:Key。
除了提供 x:Key 的默認值之外,TargetType 屬性還指定要應用 setter 屬性的類型。如果未指定 TargetType,則必須通過語法 Property="ClassName.Property",用類名限定 Setter 對象的屬性。例如,必須將 Property 設置為 "TextBlock.FontSize" 或 "Control.FontSize",而不要設置 Property="FontSize"。