WPFにあってSilverlight 2 Betaにないシリーズ「プロパティ値の継承」
WPFでは特定のプロパティにおいて、親要素が持つ同名のプロパティ値を継承するようになっています。
まずはXAMLとその実行結果から見ていただきたいと思います。
XAML(WPF)
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="100" Width="300">
<Grid>
<Button FontSize="36">
<TextBlock Text="36ポイント" />
</Button>
</Grid>
</Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="100" Width="300">
<Grid>
<Button FontSize="36">
<TextBlock Text="36ポイント" />
</Button>
</Grid>
</Window>
XAML(Silverlight)
<UserControl x:Class="SilverlightPropertyValueInheritance.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="300" Height="100">
<Grid>
<Button FontSize="36">
<TextBlock Text="36ポイントにはならない" />
</Button>
</Grid>
</UserControl>
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="300" Height="100">
<Grid>
<Button FontSize="36">
<TextBlock Text="36ポイントにはならない" />
</Button>
</Grid>
</UserControl>
このようにWPFではButtonのFontSizeプロパティに設定した値がその子要素であるTextBlockに継承されていますが、Silverlight 2 Betaでは継承されずTextBlockのFontSizeプロパティはデフォルト値のままとなります。
そもそもSilverlightでは親要素のプロパティ値を継承する添付プロパティを作成することはできません。これはWPFとSilverlight 2 BetaのDependencyProperty.
WPF
- RegisterAttached(String, Type, Type)
- RegisterAttached(String, Type, Type, PropertyMetadata)
- RegisterAttached(String, Type, Type, PropertyMetadata, ValidateValueCallback)
Silverlight 2 Beta
- RegisterAttached(String, Type, Type, PropertyChangedCallback)
WPFのプロパティ値の継承は、FrameworkPropertyMetadataのメタデータオプションにInheritsを指定することで実現できますが、Silverlight 2 BetaではPropertyMetadataは存在しません。でもPropertyChangedCallbackはさすがに必要なので、これはRegisterメソッドの引数として指定するようになっています。プロパティの既定値、CoerceValueCallback、UpdateSourceTrigger、ValidateValueCallbackなどは指定できないようです。