【问题标题】:Assigning an animation to multiple objects in WPF将动画分配给 WPF 中的多个对象
【发布时间】:2012-03-15 11:40:57
【问题描述】:

我将直接解决问题。在我的代码中,有一个椭圆数组,即
Ellipse[] players = new Ellipse[11];
然后,我使用 DoubleAnimationWithKeyFrames 在画布中为这些椭圆设置动画,使用

sboard.Children.Add(anim);
sboard.Children.Add(anim2);
Storyboard.SetTargetName(anim, players[player - 1].Name);
Storyboard.SetTargetName(anim2, players[player - 1].Name);
Storyboard.SetTargetProperty(anim, new PropertyPath(Canvas.LeftProperty));
Storyboard.SetTargetProperty(anim2, new PropertyPath(Canvas.TopProperty));

但我想用这些椭圆移动一些文本块。如何将相同的 anim 和 anim2 分配给这些 TextBlock?

【问题讨论】:

    标签: c# wpf wpf-controls storyboard


    【解决方案1】:

    将 Ellipse 和 TextBlock 放入容器(网格、堆栈面板、画布等)或制作 UserControl,并为其设置动画而不是 Ellipse。

    例子:

    <UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    x:Class="WpfApplication4.UserControl1"
    x:Name="UserControl">
    
    <StackPanel x:Name="LayoutRoot">
        <Ellipse Fill="#FFF4F4F5" Height="100" Stroke="Black" Width="100"/>
        <TextBlock TextWrapping="Wrap" Text="Some Text Here" FontSize="32" HorizontalAlignment="Center"/>
    </StackPanel></UserControl>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-01
      • 2012-01-06
      • 2011-12-24
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多