Storyboard apply to all labels

Posted by ThitoO on Stack Overflow See other posts from Stack Overflow or by ThitoO
Published on 2010-03-30T15:22:44Z Indexed on 2010/03/30 16:13 UTC
Read the original article Hit count: 610

Filed under:
|
|
|

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 :)

© Stack Overflow or respective owner

Related posts about wpf

Related posts about animation