I have a button in a grid and I want it to be disabled after 5 sec. I have tried to do that by a Timer's Elapsed event and Enabled property. Here is my button -
<Window.DataContext>
<local:VM/>
</Window.DataContext>
<Grid>
<Button Content="Button" Command="{Binding ACommand}"/>
</Grid>
and I have tried with following code -
public class VM
{
Timer timer;
public Command ACommand { get; set; }
public VM()
{
timer = new Timer(5000);
timer.Start();
timer.Elapsed += disableTimer;
ACommand = new Command(Do, CanDo);
}
bool CanDo(object obj) => timer.Enabled;
void Do(object obj) { }
void disableTimer(object sender, ElapsedEventArgs e)
{
timer.Stop();
timer.Enabled = false;
}
}
It remains enabled after 5 sec.