执行PauseStoryBoard操作时抛出InvalidOperationException的继承样式

时间:2009-11-15 22:35:04

标签: .net wpf styles eventtrigger

下面的XAML执行以下操作:当鼠标位于文本块上时,其文本将放大,当鼠标离开文本块时,其文本会缩小。单击鼠标时,文本大小会冻结。

当文本框的样式直接设置为SwellingTexblock资源时,这可以正常工作。但是,当样式设置为继承自SwellingTexblock的StyleWrapper样式时,单击鼠标时会出现以下异常(荷兰语):

  

Kan de naam EnlargeFont niet binnen   het namenbereik van   System.Windows.Style vinden。

这意味着:在System.Windows.Style的名称范围内找不到EnlargeFont。发生了什么事?

<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>

1 个答案:

答案 0 :(得分:0)

似乎这种行为是“按设计”的。您必须将动画移动到controltemplate才能使其正常工作。

您可以找到完整的解决方案here