<TextBlock x:Name="xDen" Text="Wrong">
<TextBlock.Triggers>
<EventTrigger RoutedEvent="TextBlock.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="xDen" Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:0:2" AutoReverse="True" RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>你好,这是我的代码块,我有一个特殊的代码块,我在代码块中解释过,
private void Check_Click(object sender, RoutedEventArgs e)
{
if(engEdu.Text != "")
{
int indexen = wordEngs.FindIndex(a => a.Contains(engEdu.Text));
if(checkPoint.Text == wordEsps[indexen])
{
espEdu.Text = wordEsps[indexen];
}
else
{
MessageBox.Show("Wrong!");
**I want to work that animation here!**
}
}
}我应该怎么做才能解决这个问题?
我试过这样做,
private void ex()
{
// I Tried to route 'ex' and work it but I couldn't
}发布于 2021-02-21 22:22:14
你有很多可能性。例如,您可以使用EventTrigger或DataTrigger。
下面的示例定义了两个自定义路由事件:触发错误动画的ValidationFailed和停止错误动画的ValidationPassed:
MainWindow.xaml
<Window>
<Window.Triggers>
<EventTrigger RoutedEvent="MainWindow.ValidationFailed">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="xDen"
Storyboard.TargetProperty="Opacity"
From="0"
To="1"
Duration="0:0:0:2"
AutoReverse="True"
RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MainWindow.ValidationPassed">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="xDen"
Storyboard.TargetProperty="Opacity"
From="1"
To="0"
Duration="0:0:0:2" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
<TextBlock x:Name="xDen"
Text="Wrong" />
</Window>MainWindow.xaml.cs
public partial class MainWindow : Window
{
#region ValidationFailedRoutedEvent
public static readonly RoutedEvent ValidationFailedRoutedEvent = EventManager.RegisterRoutedEvent(
"ValidationFailed",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(MainWindow));
public event RoutedEventHandler ValidationFailed
{
add => AddHandler(MainWindow.ValidationFailedRoutedEvent, value);
remove => RemoveHandler(MainWindow.ValidationFailedRoutedEvent, value);
}
#endregion
#region ValidationPassedRoutedEvent
public static readonly RoutedEvent ValidationPassedRoutedEvent = EventManager.RegisterRoutedEvent(
"ValidationPassed",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(MainWindow));
public event RoutedEventHandler ValidationPassed
{
add => AddHandler(MainWindow.ValidationPassedRoutedEvent, value);
remove => RemoveHandler(MainWindow.ValidationPassedRoutedEvent, value);
}
#endregion
private void Check_Click(object sender, RoutedEventArgs e)
{
if (engEdu.Text != "")
{
int indexen = wordEngs.FindIndex(a => a.Contains(engEdu.Text));
if (checkPoint.Text == wordEsps[indexen])
{
// Stop the error animation
RaiseEvent(new RoutedEventArgs(MainWindow.ValidationPassedRoutedEvent, this));
}
else
{
// Show the error animation
RaiseEvent(new RoutedEventArgs(MainWindow.ValidationFailedRoutedEvent, this));
}
}
}
}https://stackoverflow.com/questions/66302678
复制相似问题