【发布时间】:2011-02-02 12:52:35
【问题描述】:
我想在我的窗口中的一组标签上应用一个小故事板。 我的故事板是这样的:
<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中创建的,它们是由窗口的构造函数创建的:
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(绑定到标签的名称)。它正在工作......
现在你什么都知道了。
感谢您的帮助:)
【问题讨论】:
-
我假设“myLabel”在 UniformGrid 中。当我在 XAML 中向统一网格添加两个标签时,您的代码对我有用。你能告诉我们更多关于一切是如何设置的吗?
-
编辑问题的标题不会关闭它。如果您确实希望关闭该问题,请将其标记为版主并留言。
-
好的,Aaronaught,我刚刚做到了。
标签: c# wpf animation storyboard