【问题标题】:Inherited style throwing an InvalidOperationException when executing a PauseStoryBoard action执行 PauseStoryBoard 操作时抛出 InvalidOperationException 的继承样式
【发布时间】:2009-11-15 22:35:03
【问题描述】:

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

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

Kan de naam 放大字体 niet binnen 他的名字nbereik 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>

【问题讨论】:

    标签: .net wpf styles eventtrigger


    【解决方案1】:

    这种行为似乎是“设计使然”。您必须将动画移动到控件模板才能使其工作。

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

    【讨论】:

      猜你喜欢
      • 2011-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-17
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多