我不得不一个故事板对象的实例的引用,并希望得到的框架元件,它被附接到/动画的保持。我一直没能拿出这样做的任何方式。

例如,在下面的XAML,我可以从参考到故事板来获得或者标签或网格

的保持
<Grid>
    <Grid.Resources>
        <Storyboard x:Key="myStoryboard">
            <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:5"/>
        </Storyboard>
        <Style x:Key="myStyle" TargetType="{x:Type Label}">
            <Style.Triggers>
                <DataTrigger 
                 Binding="{Binding Path=StartAnimation}" Value="true">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource myStoryboard}" />                            
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>
    <Label x:Name="labelHello" Grid.Row="0" Style="{StaticResource myStyle}">Hello</Label>
</Grid>

对于那些想知道为什么地球上我需要做到这一点,那是因为我试图创建一个派生故事板类或附加的行为,让我对DataContext的指定方法名称时,故事板完成被称为事件触发。这将让我做纯MVVM,而不需要一些代码后面叫了我的视图模型。

有帮助吗?

解决方案

如果你改变了你的XAML来是这样的:

<Grid x:Name="grid">
    <Grid.Resources>
        <Storyboard x:Key="myStoryboard">
            <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:5" Storyboard.Target="{Binding ElementName = grid}"/>
        </Storyboard>
        <Style x:Key="myStyle" TargetType="{x:Type Label}">
            <Style.Triggers>
                <DataTrigger 
                 Binding="{Binding Path=StartAnimation}" Value="true">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource myStoryboard}" />                            
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>
    <Label x:Name="labelHello" Grid.Row="0" Style="{StaticResource myStyle}">Hello</Label>
</Grid>

此介绍的x:名称到电网和Storyboard.Target到DoubleAnimation是。现在,您可以使用此代码格的引用:

Storyboard sb = //You mentioned you had a reference to this.
var timeLine = sb.Children.First();
var myGrid = Storyboard.GetTarget(timeLine);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top