WPFとSilverlight 2 Beta 2の互換性 - ToolTip
WPFではToolTipを表示する方法が大きく分けて2種類ありました。
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="300" Width="320" FontSize="20">
<StackPanel>
<Button Content="(ア)ButtonのToolTip">
<Button.ToolTip>
<ToolTip Content="依存関係プロパティ" FontSize="20"/>
</Button.ToolTip>
</Button>
<Button Content="(イ)ButtonのToolTip">
<Button.ToolTip>
<TextBlock Text="依存関係プロパティ" FontSize="20"/>
</Button.ToolTip>
</Button>
<Button Content="(ウ)ButtonのToolTip" ToolTip="依存関係プロパティ"/>
<Button Content="(エ)ToolTipServiceのToolTip">
<ToolTipService.ToolTip>
<ToolTip Content="添付プロパティ" FontSize="20"/>
</ToolTipService.ToolTip>
</Button>
<Button Content="(オ)ToolTipServiceのToolTip" ToolTipService.ToolTip="添付プロパティ"/>
<Button Content="(カ)ToolTipServiceのToolTip">
<ToolTipService.ToolTip>
<TextBlock Text="添付プロパティ" FontSize="20"/>
</ToolTipService.ToolTip>
</Button>
</StackPanel>
</Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="320" FontSize="20">
<StackPanel>
<Button Content="(ア)ButtonのToolTip">
<Button.ToolTip>
<ToolTip Content="依存関係プロパティ" FontSize="20"/>
</Button.ToolTip>
</Button>
<Button Content="(イ)ButtonのToolTip">
<Button.ToolTip>
<TextBlock Text="依存関係プロパティ" FontSize="20"/>
</Button.ToolTip>
</Button>
<Button Content="(ウ)ButtonのToolTip" ToolTip="依存関係プロパティ"/>
<Button Content="(エ)ToolTipServiceのToolTip">
<ToolTipService.ToolTip>
<ToolTip Content="添付プロパティ" FontSize="20"/>
</ToolTipService.ToolTip>
</Button>
<Button Content="(オ)ToolTipServiceのToolTip" ToolTipService.ToolTip="添付プロパティ"/>
<Button Content="(カ)ToolTipServiceのToolTip">
<ToolTipService.ToolTip>
<TextBlock Text="添付プロパティ" FontSize="20"/>
</ToolTipService.ToolTip>
</Button>
</StackPanel>
</Window>
(ア)〜(ウ)はFrameworkElement.ToolTipプロパティを使っています。これは通常の依存関係プロパティになります。
一方、(エ)〜(カ)はToolTipService.ToolTipプロパティを使っています。こちらは添付プロパティとなっており、ターゲットはDependencyObjectであるため、ほぼどこにでも定義することができます。
Silverlight 2 Beta 1では、FrameworkElementではないもののContentControlなどにToolTipプロパティが用意されていたため、すべてではないものの多くのコントロールで(ア)〜(ウ)の方法を使うことができました。Silverlight 2 Beta 2では、ContentControlなどのToolTipプロパティは削除されました。そのため、後者の方法でのみツールチップを定義することができます。また、ToolTipクラスも削除されたため、実質的に使える方法は(オ)と(カ)になります。
XAML(Silverlight 2 Beta 2)
<UserControl x:Class="SilverlightToolTip.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="320" Height="300" FontSize="20">
<StackPanel>
<Button Content="(オ)ToolTipServiceのToolTip" ToolTipService.ToolTip="添付プロパティ"/>
<Button Content="(カ)ToolTipServiceのToolTip">
<ToolTipService.ToolTip>
<TextBlock Text="添付プロパティ" FontSize="20"/>
</ToolTipService.ToolTip>
</Button>
</StackPanel>
</UserControl>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="320" Height="300" FontSize="20">
<StackPanel>
<Button Content="(オ)ToolTipServiceのToolTip" ToolTipService.ToolTip="添付プロパティ"/>
<Button Content="(カ)ToolTipServiceのToolTip">
<ToolTipService.ToolTip>
<TextBlock Text="添付プロパティ" FontSize="20"/>
</ToolTipService.ToolTip>
</Button>
</StackPanel>
</UserControl>
今後ますますWPFとSilverlightの互換性が高まりXAMLを共有するようになることを考えると、WPFでもツールチップは(オ)と(カ)の書き方をしておいた方がよさそうです。