views:

136

answers:

2

Hi everyone !

I whant to apply a little storyboard to a collection of labels in my window. My storyboard is like that :

<Storyboard x:Key="Storyboard1" AutoReverse="True" RepeatBehavior="Forever">
        <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="label" Storyboard.TargetProperty="(Label.Foreground).(SolidColorBrush.Color)">
            <SplineColorKeyFrame KeyTime="00:00:00.1000000" Value="#FFFFFF"/>
        </ColorAnimationUsingKeyFrames>
</Storyboard>

I have a window composed of that :

<Grid Background="#FF000000">
        <Viewbox HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform">
            <UniformGrid x:Name="grid" Background="#FF000000" />
        </Viewbox>
</Grid>

When I want to start my storyboard I do that :

Storyboard.SetTarget( _stb, myLabel );
_stb.Begin();

where _std is my storyboard loaded by the window's resources.

The animation works fine, but on all labels (not only the one I want). I tried to switch SetTarget by SetTargetName but labels are created into my window by the constructor and names can not be founded when I try "SetTargetName".

Do you have any ideas ?

Thanks :)

------------ Edit : We asked me to be more descriptive --------------------------------------------------------------------

Label are not created directly in the xaml, they are created by the constructor of the window :

public SpellerWindow(IKeyboard keyboard, int colomnNumber, SolidColorBrush background, SolidColorBrush foreground )
{
    InitializeComponent();
    grid.Columns = colomnNumber;
    int i = 0;
    foreach( IKey key in keyboard.Zones.Default.Keys )
    {
        Label lb = new Label();
        lb.Foreground = foreground;
        lb.Name = "label"+(i++).ToString();
        lb.Content = key.ActualKeys[keyboard.CurrentMode].UpLabel;
        lb.HorizontalAlignment = HorizontalAlignment.Center;
        lb.VerticalAlignment = VerticalAlignment.Center;

        Viewbox box = new Viewbox();
        box.Stretch = Stretch.Fill;
        box.Child = lb;
        box.Tag = key;

        grid.Children.Add( box );
    }
}

Animations are started by an event handler :

void Highlighter_StartAnimation( object sender, HiEventArgs e )
{
      Storyboard stb;
      if( !_anims.TryGetValue( e.Step.Animation.Name, out stb ) )
      {
          stb = (Storyboard)_window.FindResource( e.Step.Animation.Name );
          _anims.Add( e.Step.Animation.Name, stb );
      }

      DoAnimations( _zones[e.Step.Zone], stb );
}

Finally, animations are started by DoAnimations :

void DoAnimations( List<Label> labels, Storyboard stb )
{
     foreach( Label lb in labels )
     {
         Storyboard.SetTarget( stb, lb );
         stb.Begin();
     }
}

I want to highlight a collection of labels, but all labels are flashing. I don't know why, but I try to create a label into the Xaml directly, and set a Storyboard.TargetName (bound to the name of the label) in the Xaml of the storyboard. And it's working ...

Now you know everything.

Thanks for you help :)

A: 

The blinking is caused by the fact the storyboard has its RepeatBehavior set to forever. This means when the animation ends it starts over from the beginning, reseting the original foreground color and animating it to the ending color. What you're probably looking for is to set FillBehavior to "HoldEnd".

The reason all the labels are blinking is because you have one instance of a storyboard and hooked up all the labels to it. When the storyboard begins, all its targets will get animated. You need to add and remove the storyboards targets as needed.

Scott J
Thank you for your answer Scott.I know for the RepeatBehavior, it was just for tests.For my problem, first time I thought like you, so I tried to set only one label as the storyboard's target but all labels animate.I think I'll try to create Label directly in the Xaml, or use a XamlWriter to write Xaml from the constructor of the window.What do you think ?
ThitoO
A: 

I found the solution !

I made a mistakes in the constructor of my window :

public SpellerWindow(IKeyboard keyboard, int colomnNumber, SolidColorBrush background, SolidColorBrush foreground )
{
    ....
}

Foreach key founded in the keyboard, I create a new label, with the given background and foreground. When the animation change the foreground of a label, it will be changed on all labels, because all labels use the same reference to the SolidColorBrush.

Thanks for your help ;)

ThitoO