我想将一个小故事板应用于我窗口中的一组标签。我的故事板是这样的:
<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>
我有一个由它组成的窗口:
<Grid Background="#FF000000">
<Viewbox HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform">
<UniformGrid x:Name="grid" Background="#FF000000" />
</Viewbox>
</Grid>
当我想开始我的故事板时,我会这样做:
Storyboard.SetTarget( _stb, myLabel );
_stb.Begin();
其中 _std 是由窗口资源加载的故事板。
动画效果很好,但在所有标签上(不仅仅是我想要的)。我尝试通过 SetTargetName 切换 SetTarget,但构造函数在我的窗口中创建了标签,并且当我尝试“SetTargetName”时无法创建名称。
你有什么想法 ?
谢谢 :)
------------ 编辑:我们要求我更具描述性 ----------------------------- --------------------------------------
标签不是直接在 xaml 中创建的,它们是由 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 );
}
}
动画由事件处理程序启动:
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 );
}
最后,动画由 DoAnimations 启动:
void DoAnimations( List<Label> labels, Storyboard stb )
{
foreach( Label lb in labels )
{
Storyboard.SetTarget( stb, lb );
stb.Begin();
}
}
我想突出显示一组标签,但所有标签都在闪烁。我不知道为什么,但我尝试直接在Xaml中创建一个标签,并在情节提要的Xaml中设置一个Storyboard.TargetName(绑定到标签的名称)。它正在工作......
现在你什么都知道了。
谢谢你的帮助:)