I'm trying to create a simple change in the background color of a border based on a timer.
I have this XAML:(That I pretty much copied from this answer)
<Grid>
<Border CornerRadius="50"
x:Name="ledColor"
BorderBrush="Black"
Background="Gray"
BorderThickness="5">
<Border.Triggers>
<EventTrigger RoutedEvent="Border.MouseLeave"> //Event that triggers the animation
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="LightGreen"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
FillBehavior="Stop"
Duration="0:0:0.1"
AutoReverse="True"></ColorAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Border.Triggers>
<Viewbox Stretch="Uniform"
VerticalAlignment="Center"
HorizontalAlignment="Center">
<Label Content="Tx" />
</Viewbox>
</Border>
</Grid>
Which works just fine for the built-in MouseLeave
event. When I hover over the control and then move the mouse outside, the background color changes to green and then returns to its original state.
However, what I want to do is to trigger the animation based on a custom event.
In the code behind I made a timer with a 1000ms interval. I want to trigger the animation on the timer's 'Elapsed' event.
public partial class LedIndicator : UserControl
{
private System.Timers.Timer _timer;
public Timer Timer { get => _timer; set => _timer = value; }
public LedIndicator()
{
InitializeComponent();
Timer = new System.Timers.Timer
{
AutoReset = true,
Interval = 1000
};
Timer.Elapsed += OnTimerElapsed;
Timer.Start();
}
public void OnTimerElapsed(object? sender, System.Timers.ElapsedEventArgs e)
{
Debug.WriteLine($"Timer elapsed");
}
}
I wrapped the timer in a Property in the hope of somehow exposing its Elapse
event to the XAML. But I have no idea how to do it.