简体   繁体   中英

Bind checkbox to Timer.Enabled

Is it possible to bind a checkbox to Enabled property of System.Timers.Timer?

I can wrap it in a boolean property which returns its state, like this:

public bool TimerEnabled
    {
        get { return _timer.Enabled; }
        set
        {
            _timer.Enabled = value;
            InvokePropertyChanged(new PropertyChangedEventArgs("TimerEnabled"));
        }
    }

But I don't get notified when _timer.Autoreset = false and _timer.Enabled changes value.

I want to have a checkbox ("EnabledCheckbox") in which shows the current state of the timer, and the user can enable or disable it. Besides, I want another checkbox ("AutoResetCheckbox") which controls wether this timer has it's Autoreset property enabled. And finally, if Autoreset is disabled and timer triggers Elapsed event and therefore sets Enabled to false, I want the "EnabledCheckbox" to uncheck on its own.

How do I achieve this?

Edit: I could make my own implementation of autoreset, but is there a better way?

Wrap _timer.Autoreset into a property like that:

public bool Autoreset
{
    get { return _timer.Autoreset; }
    set
    {
        _timer.Autoreset = value;
        InvokePropertyChanged(new PropertyChangedEventArgs(nameof(TimerEnabled));
        InvokePropertyChanged(new PropertyChangedEventArgs(nameof(Autoreset));
    }
}

And then work with the property instead of _timer.Autoreset.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM