Xaml Doubleanimation targetproperty崩溃

时间:2013-09-03 00:32:57

标签: wpf xaml

我想在画布内部将画布从画布右侧动画到画布左侧+标签大小,无限制。这意味着我的标签来自右侧,然后向左移动直至结束,然后再次重复。

这是我的xaml:

        <Canvas Margin="0, 0, 0, 20" Name="CanMain2" Height="30" Width="350"  >
        <Label x:Name="LabelNameSong" Content="Hello" >
            <Label.Resources>
                <Storyboard x:Key="Scroll">
                    <DoubleAnimation To="{Binding ActualWidth, ElementName=LabelNameSong}" Duration="00:00:10"
          Storyboard.TargetProperty="(Canvas.Right)"
          Storyboard.TargetName="LabelNameSong"
          RepeatBehavior="Forever"/>
                </Storyboard>
            </Label.Resources>

            <Label.Triggers>
                <EventTrigger RoutedEvent="Label.Loaded">
                    <BeginStoryboard Storyboard="{StaticResource Scroll}" />
                </EventTrigger>
                <EventTrigger RoutedEvent="Label.SizeChanged">
                    <BeginStoryboard Storyboard="{StaticResource Scroll}" />
                </EventTrigger>
            </Label.Triggers>
        </Label>
    </Canvas>

一旦我启动应用程序就崩溃了,调试器说:

  

无法为a上的'Right'属性设置动画   'System.Windows.Controls.Label'使用了   'System.Windows.Media.Animation.DoubleAnimation'。详情请见   内在异常。

我是xaml的新手,似乎无法使其发挥作用。

1 个答案:

答案 0 :(得分:1)

这是因为Canvas.RightAttachedProperty,但您尚未将该属性附加到Label

如果您将AttachedProperty添加到Label,则会让您为该值设置动画,因为该属性将注册(附加)到Label

示例:

  <Canvas Margin="0, 0, 0, 20" Name="CanMain2" Height="30" Width="350"  >
        <Label x:Name="LabelNameSong" Content="Hello" Canvas.Right="0" >
            <Label.Resources>
                <Storyboard x:Key="Scroll">
                    <DoubleAnimation To="{Binding ActualWidth, ElementName=LabelNameSong}" Duration="00:00:10"
          Storyboard.TargetProperty="(Canvas.Right)"
          Storyboard.TargetName="LabelNameSong"
          RepeatBehavior="Forever"/>
                </Storyboard>
            </Label.Resources>

            <Label.Triggers>
                <EventTrigger RoutedEvent="Label.Loaded">
                    <BeginStoryboard Storyboard="{StaticResource Scroll}" />
                </EventTrigger>
                <EventTrigger RoutedEvent="Label.SizeChanged">
                    <BeginStoryboard Storyboard="{StaticResource Scroll}" />
                </EventTrigger>
            </Label.Triggers>
        </Label>
    </Canvas>