简体   繁体   English

故事板适用于所有标签

[英]Storyboard apply to all labels

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. _std是窗口资源加载的故事板。

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". 我试图通过SetTargetName切换SetTarget,但是标签是由构造函数创建到我的窗口中的,当我尝试“ 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 : 标签不是直接在xaml中创建的,它们是由窗口的构造函数创建的:

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 : 最后,动画由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. 我不知道为什么,但是我尝试直接在Xaml中创建标签,并在情节提要的Xaml中设置Storyboard.TargetName(绑定到标签的名称)。 And it's working ... 它正在工作...

Now you know everything. 现在,您知道了一切。

Thanks for you help :) 谢谢您的帮助:)

The blinking is caused by the fact the storyboard has its RepeatBehavior set to forever. 闪烁是由于情节提要板将其RepeatBehavior设置为永久。 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". 您可能正在寻找的是将FillBehavior设置为“ 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. 您需要根据需要添加和删除情节提要目标。

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. 我在键盘中建立了Foreach键,并使用给定的背景和前景创建了一个新标签。 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. 当动画更改标签的前景时,它将在所有标签上更改,因为所有标签都使用对SolidColorBrush的相同引用。

Thanks for your help ;) 谢谢你的帮助 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM