WPF故事板动画。无法将数据绑定到SeekStoryboard偏移量。需要想法

WPF故事板动画。无法将数据绑定到SeekStoryboard偏移量。需要想法,wpf,storyboard,Wpf,Storyboard,我创建了一个用于ListBoxItems的DataTemplate。我创建了一个彩色动画,其中项目的背景色开始为绿色,并在一段时间内慢慢变为红色 问题是——我需要根据列表框项目中包含的时间戳设置情节提要搜索偏移量(例如:一个项目突然填充了列表框,其彩色动画应该移动,比如说,移动到动画中17秒)。当我最初创建它时,我认为这将非常容易,因为我只需将偏移量绑定到一个值转换器,该转换器根据绑定项的时间戳返回一个TimeSpan值。但是,正如我所发现的,您不能对SeekStoryboard偏移执行数据绑定

我创建了一个用于ListBoxItems的DataTemplate。我创建了一个彩色动画,其中项目的背景色开始为绿色,并在一段时间内慢慢变为红色

问题是——我需要根据列表框项目中包含的时间戳设置情节提要搜索偏移量(例如:一个项目突然填充了列表框,其彩色动画应该移动,比如说,移动到动画中17秒)。当我最初创建它时,我认为这将非常容易,因为我只需将偏移量绑定到一个值转换器,该转换器根据绑定项的时间戳返回一个TimeSpan值。但是,正如我所发现的,您不能对SeekStoryboard偏移执行数据绑定

有人能建议一个解决方法或者一些魔法背后的代码来解决这个问题吗?此列表框将包含很少的项(最多5项),因此迭代集合并以编程方式获取情节提要资源并手动设置偏移量不会太耗时。但是,我不确定如何执行此操作,因为我的Listbox由自定义对象填充,而不是ListBoxItem类型的UIElement

    <DataTemplate x:Key="TicketTemplate">
        <DataTemplate.Resources>
            <Storyboard x:Key="OnLoaded1">
                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border">
                    <EasingColorKeyFrame KeyTime="0" Value="Lime"/>
                    <EasingColorKeyFrame KeyTime="0:15:0" Value="Yellow"/>
                    <EasingColorKeyFrame KeyTime="0:30:0" Value="Orange"/>
                    <EasingColorKeyFrame KeyTime="0:45:0" Value="Red"/>
                </ColorAnimationUsingKeyFrames>
            </Storyboard>
        </DataTemplate.Resources>
        <Border x:Name="border" BorderBrush="Black" Margin="1" BorderThickness="2" CornerRadius="10" Padding="5,1">
            <Border.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="GhostWhite"  Offset="1"/>
                    <GradientStop Color="GhostWhite"/>
                </LinearGradientBrush>
            </Border.Background>
            <Grid>
                <!-- ELEMENTS FOR LISTBOXITEM TEMPLATE -->
            </Grid>
        </Border>
        <DataTemplate.Triggers>
            <EventTrigger RoutedEvent="FrameworkElement.Loaded" SourceName="border">
                <BeginStoryboard x:Name="WarningStory" Storyboard="{StaticResource OnLoaded1}"/>
                <SeekStoryboard BeginStoryboardName="WarningStory" Offset="0" <!-- cannot databind to Offset =( --> Origin="BeginTime" />
            </EventTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>


有一种变通方法可以启用绑定到非依赖性属性的属性。它要求拥有目标属性的类派生自
DependencyObject
,幸运的是
SeekStoryboard

您可以使用设置实际目标属性的

public static class SeekStoryboardExt
{
    public static readonly DependencyProperty BindableOffsetProperty =
        DependencyProperty.RegisterAttached(
            "BindableOffset", typeof(TimeSpan), typeof(SeekStoryboardExt),
            new PropertyMetadata(BindableOffsetPropertyChanged));

    public static TimeSpan GetBindableOffset(DependencyObject obj)
    {
        return (TimeSpan)obj.GetValue(BindableOffsetProperty);
    }

    public static void SetBindableOffset(DependencyObject obj, TimeSpan value)
    {
        obj.SetValue(BindableOffsetProperty, value);
    }

    private static void BindableOffsetPropertyChanged(
        DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        var seekStoryboard = obj as SeekStoryboard;
        if (seekStoryboard != null)
        {
            seekStoryboard.Offset = (TimeSpan)e.NewValue;
        }
    }
}
现在,您将绑定helper属性而不是target属性:

<SeekStoryboard local:SeekStoryboardExt.BindableOffset="{Binding ...}" .../>