8

为什么 Storyboard.SetTargetName 有效但 Storyboard.SetTarget 无效?这里 xaml -

    <Grid Grid.Row="0" ClipToBounds="True">
        <X:SmartContentControl  x:Name="smartContent"  Content="{Binding Path=MainContent}" ContentChanging="smartContent_ContentChanging">
            <X:SmartContentControl.RenderTransform>
                <TranslateTransform x:Name="translateTransformNew" X="0" Y="0"/>
            </X:SmartContentControl.RenderTransform>
        </X:SmartContentControl>
        <ContentControl Content="{Binding ElementName=smartContent, Path=LastImage}">
            <ContentControl.RenderTransform>
                <TranslateTransform x:Name="translateTransformLast" X="0" Y="0"/>
            </ContentControl.RenderTransform>
        </ContentControl>
    </Grid>

这里 C#

private void smartContent_ContentChanging(object sender, RoutedEventArgs e)
{
    Storyboard storyBoard = new Storyboard();
    DoubleAnimation doubleAnimation1 = new DoubleAnimation(0.0, -smartContent.RenderSize.Width, new Duration(new TimeSpan(0, 0, 0, 0, 500)));
    DoubleAnimation doubleAnimation2 = new DoubleAnimation(smartContent.RenderSize.Width, 0.0, new Duration(new TimeSpan(0, 0, 0, 0, 500)));

    doubleAnimation1.AccelerationRatio = 0.5;
    doubleAnimation2.DecelerationRatio = 0.5;
    storyBoard.Children.Add(doubleAnimation1);
    storyBoard.Children.Add(doubleAnimation2);
    Storyboard.SetTarget(doubleAnimation1, this.translateTransformLast); //--- this does not work
    //Storyboard.SetTargetName(doubleAnimation1, "translateTransformLast"); -- this works
    Storyboard.SetTargetProperty(doubleAnimation1, new PropertyPath(TranslateTransform.XProperty));
    Storyboard.SetTarget(doubleAnimation2, this.translateTransformNew);//--- this does not work
    //Storyboard.SetTargetName(doubleAnimation2, "translateTransformNew"); -- this works
    Storyboard.SetTargetProperty(doubleAnimation2, new PropertyPath(TranslateTransform.XProperty));
    if (smartContent.LastImage != null)
        storyBoard.Begin();
}
4

1 回答 1

7

我在这里找到了答案! 为什么当我使用故事板时这些动画不起作用?

故事板无法为 TranslateTransform 设置动画,因为它不是 UIElement。我现在就是这样做的!:)

  Storyboard.SetTarget(doubleAnimation1, this.lastImage);
    Storyboard.SetTargetProperty(doubleAnimation1, new PropertyPath("RenderTransform.(TranslateTransform.X)"));

    Storyboard.SetTarget(doubleAnimation2, this.smartContent);
    Storyboard.SetTargetProperty(doubleAnimation2, new PropertyPath("RenderTransform.(TranslateTransform.X)"));
于 2010-06-02T14:10:54.340 回答