1

I would like to animate my Label inside of canvas from canvas right to canvas left + size of label, infinitely. Which means my label comes from the right side and goes to the left till the end and then again repeats.

This is my 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>

As soon as I launch app it crashes, and debugger says:

Cannot animate the 'Right' property on a 'System.Windows.Controls.Label' using a 'System.Windows.Media.Animation.DoubleAnimation'. For details see the inner exception.

I am new to xaml and can't seem to make this work.

4

1 回答 1

1

这是因为Canvas.Right是一个,AttachedProperty但您没有将该属性附加到您的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>
于 2013-09-03T01:11:20.227 回答