我已经生成了一个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:
<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 -使用完成事件来停止或删除故事板,像这样:
<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();
之前