RoutedEvent from XAML

331 Views Asked by At
<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>

Hello, it's my code block and I have code a special code block like, I explained in code block,

        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!**
            }
        }
    }

what should I do for solving this problem?

I tried to do like that,

private void ex()
{
  // I Tried to route 'ex' and work it but I couldn't
}
1

There are 1 best solutions below

9
BionicCode On

You have many possibilities. You can use EventTrigger or DataTrigger for instance.

The following example defines two custom routed events: ValidationFailed to trigger the error animation and ValidationPassed to stop it:

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));
      }
    }
  }
}