简体   繁体   English

将复选框绑定到Timer.Enabled

[英]Bind checkbox to Timer.Enabled

Is it possible to bind a checkbox to Enabled property of System.Timers.Timer? 是否可以将复选框绑定到System.Timers.Timer的Enabled属性?

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. 但是当_timer.Autoreset = false和_timer.Enabled更改值时,我没有收到通知。

I want to have a checkbox ("EnabledCheckbox") in which shows the current state of the timer, and the user can enable or disable it. 我想要一个复选框(“ EnabledCheckbox”),其中显示计时器的当前状态,用户可以启用或禁用它。 Besides, I want another checkbox ("AutoResetCheckbox") which controls wether this timer has it's Autoreset property enabled. 此外,我需要另一个复选框(“ AutoResetCheckbox”)来控制此计时器是否启用了Autoreset属性。 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. 最后,如果禁用了自动重置并且计时器触发了Elapsed事件,因此将Enabled设置为false,我希望“ EnabledCheckbox”自行取消选中。

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: 将_timer.Autoreset包装到这样的属性中:

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. 然后使用该属性而不是_timer.Autoreset。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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