WPFにあってSilverlight 2 Betaにないシリーズ「スタイルの継承」と「型指定によるスタイルの適用」
いまさらながらに気がついたのですが、このシリーズは結局WPFの機能を説明することになってしまい全然Silverlightを紹介することができませんね。ただ、Silverlightにはこれとこれとこれがありませんという風に書いてもあまりおもしろくありませんので、今までこのblogで紹介していなかったWPFの機能を中心に、なんとかこのシリーズを続けていきたいと思います。
今回紹介するSilverlight 2 BetaにはないWPFの機能は、「スタイルの継承」と「型指定によるスタイルの適用」です。まずはXAMLと実行結果から先に見てもらいたいと思います。
XAML(WPF)
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Window1" x:Name="Window" Title="Window1"
Width="300" Height="300">
<StackPanel x:Name="LayoutRoot" Orientation="Horizontal" HorizontalAlignment="Center">
<StackPanel>
<StackPanel.Resources>
<Style x:Key="BaseStyle" TargetType="{x:Type Rectangle}">
<Setter Property="Height" Value="100"/>
<Setter Property="Width" Value="100"/>
<Setter Property="Margin" Value="10"/>
</Style>
<Style x:Key="RectStyle1" TargetType="{x:Type Rectangle}" BasedOn="{StaticResource BaseStyle}">
<Setter Property="Fill" Value="Red"/>
</Style>
<Style x:Key="RectStyle2" TargetType="{x:Type Rectangle}" BasedOn="{StaticResource BaseStyle}">
<Setter Property="Fill" Value="Blue"/>
</Style>
</StackPanel.Resources>
<TextBlock Text="スタイルの継承"/>
<Rectangle Style="{StaticResource RectStyle1}"/>
<Rectangle Style="{StaticResource RectStyle2}"/>
</StackPanel>
<Border BorderBrush="Black" BorderThickness="1"/>
<StackPanel>
<StackPanel.Resources>
<Style x:Key="{x:Type Rectangle}" TargetType="{x:Type Rectangle}">
<Setter Property="Height" Value="100"/>
<Setter Property="Width" Value="100"/>
<Setter Property="Margin" Value="10"/>
</Style>
</StackPanel.Resources>
<TextBlock Text="型指定でスタイルを適用"/>
<Rectangle Fill="Red"/>
<Rectangle Fill="Blue"/>
</StackPanel>
</StackPanel>
</Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Window1" x:Name="Window" Title="Window1"
Width="300" Height="300">
<StackPanel x:Name="LayoutRoot" Orientation="Horizontal" HorizontalAlignment="Center">
<StackPanel>
<StackPanel.Resources>
<Style x:Key="BaseStyle" TargetType="{x:Type Rectangle}">
<Setter Property="Height" Value="100"/>
<Setter Property="Width" Value="100"/>
<Setter Property="Margin" Value="10"/>
</Style>
<Style x:Key="RectStyle1" TargetType="{x:Type Rectangle}" BasedOn="{StaticResource BaseStyle}">
<Setter Property="Fill" Value="Red"/>
</Style>
<Style x:Key="RectStyle2" TargetType="{x:Type Rectangle}" BasedOn="{StaticResource BaseStyle}">
<Setter Property="Fill" Value="Blue"/>
</Style>
</StackPanel.Resources>
<TextBlock Text="スタイルの継承"/>
<Rectangle Style="{StaticResource RectStyle1}"/>
<Rectangle Style="{StaticResource RectStyle2}"/>
</StackPanel>
<Border BorderBrush="Black" BorderThickness="1"/>
<StackPanel>
<StackPanel.Resources>
<Style x:Key="{x:Type Rectangle}" TargetType="{x:Type Rectangle}">
<Setter Property="Height" Value="100"/>
<Setter Property="Width" Value="100"/>
<Setter Property="Margin" Value="10"/>
</Style>
</StackPanel.Resources>
<TextBlock Text="型指定でスタイルを適用"/>
<Rectangle Fill="Red"/>
<Rectangle Fill="Blue"/>
</StackPanel>
</StackPanel>
</Window>
・スタイルの継承
WPFのStyleクラスにはBasedOnプロパティが存在し、このプロパティに継承したいスタイルを指定してやることでスタイルを継承することができます。Silverlight 2 Betaにはこのプロパティがありませんので、スタイルを継承することはできません。
・指定した型すべてにスタイルを適用
WPFではスタイルのキーに型を指定してやることで、いちいちスタイルを設定しなくともすべての同じ型を持つコントロールにスタイルを適用させることができます。上記の例では、x:Key属性に"{x:Type Rectangle}"と設定していますが、TargetTypeプロパティに型を指定している場合にはx:Key属性を省略しても同じ結果を得ることができます。
Silverlight 2 Betaでは、スタイルは1つ1つStyleプロパティを使って適用するしかありません。