Uygulamalarımızda bulunanan elementlerimizin görünüşü değiştirmek için XAML’da stilleri kullanırız.Diyelim ki uygulamamızda bulunan butonlara stil vermek istiyoruz.Örnek olarak şu şekilde bir stilimiz olduğunu varsayalım:
<Window.Resources>
<Style x:Key="buttonStyle" TargetType="{x:Type Button}">
<Setter Property="FontSize" Value="24"/>
<Setter Property="Foreground" Value="ForestGreen"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</Window.Resources>
Yukarıdaki stilimizi butonlara uygulamak içinse her bir butonun Style property’sinde hazırlamış olduğumuz stili aşağıdaki gibi belirtmemiz gerekir:
<Button Content="Click" Style="{StaticResource buttonStyle}"/>
Eğer uygulamamızdaki bütün butonlara aynı stili tek tek yerine hepsine aynı anda uygulamak istiyorsak stilimizdeki
x:Key attribute’unu kaldırmamız yeterli olacaktır.Sadece
TargetType belirterek o tiple eşleşen tüm elementlere stili otomatik uygulatabiliriz.Bunu yapmanın diğer bir yolu ise TargetType attribute’unu kaldırıp x:Key’de element tipini belirtmektir.
1.yol
<Style TargetType="{x:Type Button}">
<Setter Property="FontSize" Value="24"/>
<Setter Property="Foreground" Value="ForestGreen"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
2.yol
<Style x:Key="{x:Type Button}">
<Setter Property="Button.FontSize" Value="24"/>
<Setter Property="Button.Foreground" Value="ForestGreen"/>
<Setter Property="Button.FontWeight" Value="Bold"/>
</Style>
Gördüğünüz gibi kolayca stillerinizi otomatik olarak elementlere uygulayabilirsiniz.Hatta stil uygulanmasını istemediğiniz kontrolleri
<Button Content="Click" Style="{x:Null}"/>
şeklinde belirtebilirsiniz.İlk yolu deneyerek yazdığım uygulamanın XAML kodunu ve çıktısını bulabilirsiniz.
<StackPanel>
<Button Content="Click" Width="100" Height="50" />
<Button Content="Click" Width="100" Height="50" Style="{x:Null}"/>
<Button Content="Click" Width="100" Height="50"/>
</StackPanel>
Kolay gelsin.