Создайте пульсирующий TextBlock с раскадровкой и DataTrigger - исключение в анимации

У меня есть TextBlock, который отображает изображение из шрифта. Я хочу, чтобы TextBlock пульсировал, когда в ViewModel установлен логический флаг. Я нашел ответ Криса В. в этом аналогичном вопросе Stackoverflow. Вторая коробка в его примере - это как раз то, что мне нужно. Пульсирующий пример в основном:

<Storyboard x:Key="Pulse">
    <DoubleAnimationUsingKeyFrames 
            Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
            Storyboard.TargetName="PulseBox"
        >
        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1.15"/>
        <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
    </DoubleAnimationUsingKeyFrames>

    <DoubleAnimationUsingKeyFrames
            Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
            Storyboard.TargetName="PulseBox"
        >
        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1.15"/>
        <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>

Поскольку я ничего не знал о триггерах и раскадровках в WPF, я попытался поместить это в DataTrigger, но безуспешно.

Я придумал это:

<TextBlock Text="SomeCodeToAImageInAFont" Background="CornflowerBlue">
    <TextBlock.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Activate}" Value="True">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                             <Storyboard>
                                 <DoubleAnimationUsingKeyFrames 
                                         Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
                                 >
                                     <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1.15"/>
                                     <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
                                </DoubleAnimationUsingKeyFrames>

                                <DoubleAnimationUsingKeyFrames
                                     Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
                                >
                                    <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1.15"/>
                                    <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

Activate - это логическое свойство во ViewModel.

Когда я устанавливаю Activate bool на True, я получаю следующее исключение:

System.InvalidOperationException
The property [Unknown] does not point to a DependencyObject in the path (0).(1)[0].(2).

Совершенно очевидно, что эти строки неверны:

Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"

К сожалению, я не понимаю, что это за исключение и как его исправить.

Какие-либо предложения?


person Mc_Topaz    schedule 27.04.2021    source источник


Ответы (1)


Поскольку вы получаете доступ к преобразованию RenderTransform в своей анимации, вы должны добавить его к элементу. Я также изменил RepeatBehavior на выполнение 10 раз, или вы можете установить его на RepeatBehavior="Forever".

<TextBlock Text="SomeCodeToAImageInAFont" Background="CornflowerBlue">
        <TextBlock.RenderTransform>
            <TransformGroup>
                <ScaleTransform/>
            </TransformGroup>
        </TextBlock.RenderTransform>
    <TextBlock.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Activate}" Value="True">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimationUsingKeyFrames RepeatBehavior="10x"
                                    Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
                            >
                                    <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1.15"/>
                                    <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
                                </DoubleAnimationUsingKeyFrames>

                                <DoubleAnimationUsingKeyFrames RepeatBehavior="10x"
                                Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
                        >
                                    <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1.15"/>
                                    <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>
person Rekshino    schedule 27.04.2021