0

I am defining a storyboard as a Grid Resource. I have named it. I can set the TargetName property in code, but I cannot figure out how to set the From and To values of the storyboard in code. These values need to be set at runtime. Here is what I have.

<Grid.Resources>
                <Storyboard x:Name="btnClick_SB" >
                    <DoubleAnimation Storyboard.TargetProperty="X2" From="10" To="400" Duration="0:0:1.0" EnableDependentAnimation="True"/>
                    <DoubleAnimation Storyboard.TargetProperty="Y2" From="10" To="400" Duration="0:0:1.0" EnableDependentAnimation="True"/>
                </Storyboard>

...

<Canvas x:Name="cnvBL" Margin="0" Grid.Row="2" Background="White">
                <Line x:Name="lineBL1" Stroke="Blue" StrokeThickness="5" />
                <Line x:Name="lineBL2" Stroke="Blue" StrokeThickness="5" />
            </Canvas>

and the code

lineBL1.X1 = 10;
lineBL1.Y1 = 10;
btnClick_SB.Stop();

btnClick_SB.SetValue(Storyboard.TargetNameProperty, "lineBL1");            
btnClick_SB.Begin();

As you can see, From and To are set in XAML. I want to instead set those values in code. How can I do that?

4

1 回答 1

0

我想到了。Storyboard 没有 From 和 To 属性。它是具有 From 和 To 属性的 DoubleAnimation。因此,在 XAML 中,我只是命名了 DoubleAnimations 并删除了 From 和 To 属性设置。然后在代码中,我可以引用 DoubleAnimations 并设置它们的属性。

于 2015-11-16T13:53:35.230 回答