C#Storyboard动画不会重置位置

时间:2015-08-04 13:07:41

标签: c# storyboard wpf-animation doubleanimation

我已经生成了一个XAML文件,其中包含带有学生名和图表的滚动查看器。当我单击其中一个名称时,相应的线条将从左上方飞入(从小到正确的大小),并停在轴上的右侧位置。

当我点击另一个名字时,它会触发同样的事情,但是当它们停在正确的位置时,前面的线条会从屏幕中飞出并从右下方缩小。

这一切都有效,直到我按下之前已经按过的名字。然后这些线开始变小,保持小,并立即从右下方飞出,而不是停在具有适当尺寸的轴上。

我已尝试停止播放动画,但似乎无法正常工作,我们非常感谢任何帮助。

private void selectStudent(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        Duration duration = new Duration(new TimeSpan(0, 0, 2));

        if (line01 != null)
        {
            line1Animation1.BeginTime = new TimeSpan(0, 0, 2);
            line1Animation1.Duration = duration;
            line1Animation1.From = line01.X1;
            line1Animation1.To = (line01.X1 * 0.1) + 800;
            line1Animation2.BeginTime = new TimeSpan(0, 0, 2);
            line1Animation2.Duration = duration;
            line1Animation2.From = line01.Y1;
            line1Animation2.To = (line01.Y1 * 0.1) + 800;
            line1Animation3.BeginTime = new TimeSpan(0, 0, 2);
            line1Animation3.Duration = duration;
            line1Animation3.From = line01.X2;
            line1Animation3.To = (line01.X2 * 0.1) + 800;
            line1Animation4.BeginTime = new TimeSpan(0, 0, 2);
            line1Animation4.Duration = duration;
            line1Animation4.From = line01.Y2;
            line1Animation4.To = (line01.Y2 * 0.1) + 800;
            storyboard1.Begin();

            line2Animation1.BeginTime = new TimeSpan(0, 0, 2);
            line2Animation1.Duration = duration;
            line2Animation1.From = line02.X1;
            line2Animation1.To = (line02.X1 * 0.1) + 800;
            line2Animation2.BeginTime = new TimeSpan(0, 0, 2);
            line2Animation2.Duration = duration;
            line2Animation2.From = line02.Y1;
            line2Animation2.To = (line02.Y1 * 0.1) + 800;
            line2Animation3.BeginTime = new TimeSpan(0, 0, 2);
            line2Animation3.Duration = duration;
            line2Animation3.From = line02.X2;
            line2Animation3.To = (line02.X2 * 0.1) + 800;
            line2Animation4.BeginTime = new TimeSpan(0, 0, 2);
            line2Animation4.Duration = duration;
            line2Animation4.From = line02.Y2;
            line2Animation4.To = (line02.Y2 * 0.1) + 800;
            storyboard2.Begin();

            line3Animation1.BeginTime = new TimeSpan(0, 0, 2);
            line3Animation1.Duration = duration;
            line3Animation1.From = line03.X1;
            line3Animation1.To = (line03.X1 * 0.1) + 800;
            line3Animation2.BeginTime = new TimeSpan(0, 0, 2);
            line3Animation2.Duration = duration;
            line3Animation2.From = line03.Y1;
            line3Animation2.To = (line03.Y1 * 0.1) + 800;
            line3Animation3.BeginTime = new TimeSpan(0, 0, 2);
            line3Animation3.Duration = duration;
            line3Animation3.From = line03.X2;
            line3Animation3.To = (line03.X2 * 0.1) + 800;
            line3Animation4.BeginTime = new TimeSpan(0, 0, 2);
            line3Animation4.Duration = duration;
            line3Animation4.From = line03.Y2;
            line3Animation4.To = (line03.Y2 * 0.1) + 800;
            storyboard3.Begin();

            line4Animation1.BeginTime = new TimeSpan(0, 0, 2);
            line4Animation1.Duration = duration;
            line4Animation1.From = line04.X1;
            line4Animation1.To = (line04.X1 * 0.1) + 800;
            line4Animation2.BeginTime = new TimeSpan(0, 0, 2);
            line4Animation2.Duration = duration;
            line4Animation2.From = line04.Y1;
            line4Animation2.To = (line04.Y1 * 0.1) + 800;
            line4Animation3.BeginTime = new TimeSpan(0, 0, 2);
            line4Animation3.Duration = duration;
            line4Animation3.From = line04.X2;
            line4Animation3.To = (line04.X2 * 0.1) + 800;
            line4Animation4.BeginTime = new TimeSpan(0, 0, 2);
            line4Animation4.Duration = duration;
            line4Animation4.From = line04.Y2;
            line4Animation4.To = (line04.Y2 * 0.1) + 800;
            storyboard4.Begin();

            titleActive.Visibility = Visibility.Collapsed;
        }

        line01 = (Line)FindName(((Border)sender).Name + "_Line_1");
        storyboard1 = (Storyboard)FindName(line01.Name + "_StoryboardLine");
        line1Animation1String = line01.Name + "_lineAnimation1";
        line1Animation1 = (DoubleAnimation)FindName(line1Animation1String);
        line1Animation2String = line01.Name + "_lineAnimation2";
        line1Animation2 = (DoubleAnimation)FindName(line1Animation2String);
        line1Animation3String = line01.Name + "_lineAnimation3";
        line1Animation3 = (DoubleAnimation)FindName(line1Animation3String);
        line1Animation4String = line01.Name + "_lineAnimation4";
        line1Animation4 = (DoubleAnimation)FindName(line1Animation4String);

        line02 = (Line)FindName(((Border)sender).Name + "_Line_2");
        storyboard2 = (Storyboard)FindName(line02.Name + "_StoryboardLine");
        line2Animation1String = line02.Name + "_lineAnimation1";
        line2Animation1 = (DoubleAnimation)FindName(line2Animation1String);
        line2Animation2String = line02.Name + "_lineAnimation2";
        line2Animation2 = (DoubleAnimation)FindName(line2Animation2String);
        line2Animation3String = line02.Name + "_lineAnimation3";
        line2Animation3 = (DoubleAnimation)FindName(line2Animation3String);
        line2Animation4String = line02.Name + "_lineAnimation4";
        line2Animation4 = (DoubleAnimation)FindName(line2Animation4String);

        line03 = (Line)FindName(((Border)sender).Name + "_Line_3");
        storyboard3 = (Storyboard)FindName(line03.Name + "_StoryboardLine");
        line3Animation1String = line03.Name + "_lineAnimation1";
        line3Animation1 = (DoubleAnimation)FindName(line3Animation1String);
        line3Animation2String = line03.Name + "_lineAnimation2";
        line3Animation2 = (DoubleAnimation)FindName(line3Animation2String);
        line3Animation3String = line03.Name + "_lineAnimation3";
        line3Animation3 = (DoubleAnimation)FindName(line3Animation3String);
        line3Animation4String = line03.Name + "_lineAnimation4";
        line3Animation4 = (DoubleAnimation)FindName(line3Animation4String);

        line04 = (Line)FindName(((Border)sender).Name + "_Line_4");
        storyboard4 = (Storyboard)FindName(line04.Name + "_StoryboardLine");
        line4Animation1String = line04.Name + "_lineAnimation1";
        line4Animation1 = (DoubleAnimation)FindName(line4Animation1String);
        line4Animation2String = line04.Name + "_lineAnimation2";
        line4Animation2 = (DoubleAnimation)FindName(line4Animation2String);
        line4Animation3String = line04.Name + "_lineAnimation3";
        line4Animation3 = (DoubleAnimation)FindName(line4Animation3String);
        line4Animation4String = line04.Name + "_lineAnimation4";
        line4Animation4 = (DoubleAnimation)FindName(line4Animation4String);

        titleActive = (TextBlock)FindName(((Border)sender).Name + "_Title");

        line1Animation1.Duration = duration;
        line1Animation1.From = (line01.X1 - 250) * 0.1;
        line1Animation1.To = line01.X1;
        line1Animation2.Duration = duration;
        line1Animation2.From = (line01.Y1 - 250) * 0.1;
        line1Animation2.To = line01.Y1;
        line1Animation3.Duration = duration;
        line1Animation3.From = (line01.X2 - 250) * 0.1;
        line1Animation3.To = line01.X2;
        line1Animation4.Duration = duration;
        line1Animation4.From = (line01.Y2 - 250) * 0.1;
        line1Animation4.To = line01.Y2;
        storyboard1.Begin();

        line2Animation1.Duration = duration;
        line2Animation1.From = (line02.X1 - 250) * 0.1;
        line2Animation1.To = line02.X1;
        line2Animation2.Duration = duration;
        line2Animation2.From = (line02.Y1 - 250) * 0.1;
        line2Animation2.To = line02.Y1;
        line2Animation3.Duration = duration;
        line2Animation3.From = (line02.X2 - 250) * 0.1;
        line2Animation3.To = line02.X2;
        line2Animation4.Duration = duration;
        line2Animation4.From = (line02.Y2 - 250) * 0.1;
        line2Animation4.To = line02.Y2;
        storyboard2.Begin();

        line3Animation1.Duration = duration;
        line3Animation1.From = (line03.X1 - 250) * 0.1;
        line3Animation1.To = line03.X1;
        line3Animation2.Duration = duration;
        line3Animation2.From = (line03.Y1 - 250) * 0.1;
        line3Animation2.To = line03.Y1;
        line3Animation3.Duration = duration;
        line3Animation3.From = (line03.X2 - 250) * 0.1;
        line3Animation3.To = line03.X2;
        line3Animation4.Duration = duration;
        line3Animation4.From = (line03.Y2 - 250) * 0.1;
        line3Animation4.To = line03.Y2;
        storyboard3.Begin();

        line4Animation1.Duration = duration;
        line4Animation1.From = (line04.X1 - 250) * 0.1;
        line4Animation1.To = line04.X1;
        line4Animation2.Duration = duration;
        line4Animation2.From = (line04.Y1 - 250) * 0.1;
        line4Animation2.To = line04.Y1;
        line4Animation3.Duration = duration;
        line4Animation3.From = (line04.X2 - 250) * 0.1;
        line4Animation3.To = line04.X2;
        line4Animation4.Duration = duration;
        line4Animation4.From = (line04.Y2 - 250) * 0.1;
        line4Animation4.To = line04.Y2;
        storyboard4.Begin();

        line01.Visibility = Visibility.Visible;
        line02.Visibility = Visibility.Visible;
        line03.Visibility = Visibility.Visible;
        line04.Visibility = Visibility.Visible;
        titleActive.Visibility = Visibility.Visible;
    }
}

XAML :(来自1个人的行,不能适合整个文件)

<Line Stroke="Darkgreen" StrokeThickness="2" X1="212.97500000000002" X2="350.0" Y1="350.0" Y2="190.53124999999997" x:Name="Marilyn_Williamson_Line_1" Visibility="Collapsed">
      <Line.Triggers>
        <EventTrigger>
          <BeginStoryboard>
            <Storyboard x:Name="Marilyn_Williamson_Line_1_StoryboardLine">
              <DoubleAnimation x:Name="Marilyn_Williamson_Line_1_lineAnimation1" Storyboard.TargetName="Marilyn_Williamson_Line_1" Storyboard.TargetProperty="X1" />
              <DoubleAnimation x:Name="Marilyn_Williamson_Line_1_lineAnimation2" Storyboard.TargetName="Marilyn_Williamson_Line_1" Storyboard.TargetProperty="Y1" />
              <DoubleAnimation x:Name="Marilyn_Williamson_Line_1_lineAnimation3" Storyboard.TargetName="Marilyn_Williamson_Line_1" Storyboard.TargetProperty="X2" />
              <DoubleAnimation x:Name="Marilyn_Williamson_Line_1_lineAnimation4" Storyboard.TargetName="Marilyn_Williamson_Line_1" Storyboard.TargetProperty="Y2" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </Line.Triggers>
    </Line>
    <Line Stroke="Darkgreen" StrokeThickness="2" X1="509.86249999999995" X2="350.0" Y1="350.0" Y2="190.53124999999997" x:Name="Marilyn_Williamson_Line_2" Visibility="Collapsed">
      <Line.Triggers>
        <EventTrigger>
          <BeginStoryboard>
            <Storyboard x:Name="Marilyn_Williamson_Line_2_StoryboardLine">
              <DoubleAnimation x:Name="Marilyn_Williamson_Line_2_lineAnimation1" Storyboard.TargetName="Marilyn_Williamson_Line_2" Storyboard.TargetProperty="X1" />
              <DoubleAnimation x:Name="Marilyn_Williamson_Line_2_lineAnimation2" Storyboard.TargetName="Marilyn_Williamson_Line_2" Storyboard.TargetProperty="Y1" />
              <DoubleAnimation x:Name="Marilyn_Williamson_Line_2_lineAnimation3" Storyboard.TargetName="Marilyn_Williamson_Line_2" Storyboard.TargetProperty="X2" />
              <DoubleAnimation x:Name="Marilyn_Williamson_Line_2_lineAnimation4" Storyboard.TargetName="Marilyn_Williamson_Line_2" Storyboard.TargetProperty="Y2" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </Line.Triggers>
    </Line>
    <Line Stroke="Darkgreen" StrokeThickness="2" X1="350.0" X2="212.97500000000002" Y1="487.025" Y2="350.0" x:Name="Marilyn_Williamson_Line_3" Visibility="Collapsed">
      <Line.Triggers>
        <EventTrigger>
          <BeginStoryboard>
            <Storyboard x:Name="Marilyn_Williamson_Line_3_StoryboardLine">
              <DoubleAnimation x:Name="Marilyn_Williamson_Line_3_lineAnimation1" Storyboard.TargetName="Marilyn_Williamson_Line_3" Storyboard.TargetProperty="X1" />
              <DoubleAnimation x:Name="Marilyn_Williamson_Line_3_lineAnimation2" Storyboard.TargetName="Marilyn_Williamson_Line_3" Storyboard.TargetProperty="Y1" />
              <DoubleAnimation x:Name="Marilyn_Williamson_Line_3_lineAnimation3" Storyboard.TargetName="Marilyn_Williamson_Line_3" Storyboard.TargetProperty="X2" />
              <DoubleAnimation x:Name="Marilyn_Williamson_Line_3_lineAnimation4" Storyboard.TargetName="Marilyn_Williamson_Line_3" Storyboard.TargetProperty="Y2" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </Line.Triggers>
    </Line>
    <Line Stroke="Darkgreen" StrokeThickness="2" X1="509.86249999999995" X2="350.0" Y1="350.0" Y2="487.025" x:Name="Marilyn_Williamson_Line_4" Visibility="Collapsed">
      <Line.Triggers>
        <EventTrigger>
          <BeginStoryboard>
            <Storyboard x:Name="Marilyn_Williamson_Line_4_StoryboardLine">
              <DoubleAnimation x:Name="Marilyn_Williamson_Line_4_lineAnimation1" Storyboard.TargetName="Marilyn_Williamson_Line_4" Storyboard.TargetProperty="X1" />
              <DoubleAnimation x:Name="Marilyn_Williamson_Line_4_lineAnimation2" Storyboard.TargetName="Marilyn_Williamson_Line_4" Storyboard.TargetProperty="Y1" />
              <DoubleAnimation x:Name="Marilyn_Williamson_Line_4_lineAnimation3" Storyboard.TargetName="Marilyn_Williamson_Line_4" Storyboard.TargetProperty="X2" />
              <DoubleAnimation x:Name="Marilyn_Williamson_Line_4_lineAnimation4" Storyboard.TargetName="Marilyn_Williamson_Line_4" Storyboard.TargetProperty="Y2" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </Line.Triggers>
    </Line>

1 个答案:

答案 0 :(得分:0)

1 - 使用已完成的事件来停止或删除这样的故事板:

 <Storyboard Completed="storyboard_Completed" x:Name="Marilyn_Williamson_Line_1_StoryboardLine">

并在代码背后:

 private void storyboard_Completed(object sender, EventArgs e)
    {
        string StoryBoardName = ((ClockGroup)sender).Timeline.Name;
        if (StoryBoardName == "storyboard1") { storyboard1.Stop(); storyboard1.Remove(); }
    }

2 - 在storyboard.Stop();

之前加storyboard.Begin();