以下のXAMLは、次のことを行います。マウスがテキストブロック上にある場合、そのテキストは拡大され、マウスがテキストブロックを離れると、そのテキストは縮小されます。マウスをクリックすると、テキストサイズがフリーズします。
これは、テキストボックスのスタイルがSwellingTexblockリソースに直接設定されている場合に期待どおりに機能します。ただし、スタイルがSwellingTexblockから継承するStyleWrapperスタイルに設定されている場合、マウス(オランダ語)をクリックすると次の例外が発生します。
Kan de naam EnlargeFont niet binnen het namenbereik vanSystem.Windows.Stylevinden。
これは、次のような意味です。EnlargeFontがSystem.Windows.Styleの名前スコープに見つかりません。何が起こっている?
<Window x:Class="TriggerSpike.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="300">
<Window.Resources>
<Style x:Key="SwellingTextBlock" TargetType="TextBlock">
<Style.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard Name="EnlargeFont">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="FontSize"
To="60" Duration="0:0:10"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="FontSize"
To="12" Duration="0:0:.5"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="MouseDown">
<PauseStoryboard BeginStoryboardName="EnlargeFont"/>
</EventTrigger>
</Style.Triggers>
</Style>
<Style x:Key="StyleWrapper" BasedOn="{StaticResource SwellingTextBlock}"
TargetType="TextBlock"/>
</Window.Resources>
<StackPanel>
<TextBlock Style="{StaticResource StyleWrapper}">test</TextBlock>
</StackPanel>
</Window>