【问题标题】:WPF - Assigning storyboard begintime from parent elementWPF - 从父元素分配情节提要开始时间
【发布时间】:2010-12-26 21:56:59
【问题描述】:

我已经创建了一个基本的微调器类型控件,它工作得相当好,但其中有大量重复的代码。除了两个属性之外,它基本上是一遍又一遍的相同元素,其中一个属性是情节提要的 BeginTime - 每一行只是无休止地重复其动画,但每一行在前一个之后开始 0.1 秒小波浪效应。

下面是代码的大致思路:

<UserControl x:Class="MyNamespace.Spinner"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <Rectangle Fill="Black" Opacity="0.5"
                   Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=Width}"
                   Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=Height}" />
        <Line X1="9" X2="11" Y1="10" Y2="20">
            <Line.Triggers>
                <EventTrigger RoutedEvent="Line.Loaded">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard RepeatBehavior="Forever" Duration="0:0:0.8"
                                        BeginTime="0:0:0.1">
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity">
                                    <LinearDoubleKeyFrame Value="0.5" KeyTime="0%" />
                                    <LinearDoubleKeyFrame Value="1.0" KeyTime="12.5%" />
                                    <LinearDoubleKeyFrame Value="0.5" KeyTime="75%" />
                                    <LinearDoubleKeyFrame Value="0.5" KeyTime="100%" />
                                </DoubleAnimationUsingKeyFrames>
                             </Storyboard>
                         </BeginStoryboard>
                     </EventTrigger.Actions>
                 </EventTrigger>
             </Line.Triggers>
         </Line>
        <Line X1="14" X2="16" Y1="10" Y2="20">
            <Line.Triggers>
                <EventTrigger RoutedEvent="Line.Loaded">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard RepeatBehavior="Forever" Duration="0:0:0.8"
                                        BeginTime="0:0:0.2">
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity">
                                    <LinearDoubleKeyFrame Value="0.5" KeyTime="0%" />
                                    <LinearDoubleKeyFrame Value="1.0" KeyTime="12.5%" />
                                    <LinearDoubleKeyFrame Value="0.5" KeyTime="75%" />
                                    <LinearDoubleKeyFrame Value="0.5" KeyTime="100%" />
                                </DoubleAnimationUsingKeyFrames>
                             </Storyboard>
                         </BeginStoryboard>
                     </EventTrigger.Actions>
                 </EventTrigger>
             </Line.Triggers>
         </Line>

    <!-- And so on and so forth, 6 more times -->
    </Grid> 
</UserControl>

我不想发布整个内容,这可能看起来太痛苦了,但你明白了。从 Line.Triggers 到 /Line.Triggers 的唯一变化是情节提要的 BeginTime。 XAML 中有没有一种方法可以在 Line 中定义 BeginTime,然后在 Storyboard 中读取它,这样我就可以做一些更接近的事情:

<UserControl.Resources>
    <!-- Try to read BeginTime from ancestral element -->
    <Storyboard x:Key="myStoryboard" RepeatBehavior="Forever" Duration="0:0:0.8"
                BeginTime="{Binding SomeSource, Property=StoryboardBeginTime}">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity">
            <LinearDoubleKeyFrame Value="0.5" KeyTime="0%" />
            <LinearDoubleKeyFrame Value="1.0" KeyTime="12.5%" />
            <LinearDoubleKeyFrame Value="0.5" KeyTime="75%" />
            <LinearDoubleKeyFrame Value="0.5" KeyTime="100%" />
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</UserControl.Resources>
    <!-- Try to send BeginTime to storyboard contained within -->
    <Line Blah="blah" StoryboardBeginTime="0:0:0.1">
        <Line.Triggers>
            <EventTrigger>
                <EventTrigger.Actions>
                    <BeginStoryboard Storyboard="{StaticResource myStoryboard}" />
                </EventTrigger.Actions>
            </EventTrigger>
        </Line.Triggers>
    </Line>

我似乎无法追踪是否有办法在 XAML 中执行此操作,将故事板从树上更高的元素传递给它的属性之一的值。这种事情甚至可能吗,还是我应该在代码隐藏中做呢?如果我决定完全调整动画,这并不是现在最愉快的事情。

谢谢!

【问题讨论】:

    标签: wpf xaml binding storyboard


    【解决方案1】:

    这似乎是不可能的——虽然它可能在 4.0 中,但我没有对此进行测试。您可以使用代码隐藏来做到这一点。

    -sa

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-17
      • 2010-11-29
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      相关资源
      最近更新 更多