繁体   English   中英

绑定到无法与DispatcherTimer一起使用的属性

[英]Binding to property not working with DispatcherTimer

我有一些TextBlock绑定到具有DependencyProperty的属性。 当DispatcherTimer更改此属性时,TextBlock不会更新。 即使在调试中,我也可以看到该属性已更新,但TextBlock保持不变。

详细信息:我有一堂课:

    public class myTimer
{
    public System.DateTime Duration { get; set; }
    public System.DateTime Elapsed { get; set; }
    public System.TimeSpan Remaining {
        get {
            return Duration.Subtract(new DateTime(Duration.Year, Duration.Month, Duration.Day, Elapsed.Hour, Elapsed.Minute, Elapsed.Second));
        }
    }
}

我的xaml代码中包含myTimer类型的DependencyProperty

    public static DependencyProperty currentTimerProperty = DependencyProperty.Register("CurrentTimer", typeof(myTimer),  typeof(Question));
    public myTimer CurrentTimer
    {
        get { return (myTimer)GetValue(currentTimerProperty); }
        set { SetValue(currentTimerProperty, value); }
    }

并且我有三个TextBlock绑定到此属性:

    <TextBlock  Style="{StaticResource myTimer}">
         <TextBlock.Text>
               <MultiBinding StringFormat="{}{0:00}:{1:00;00}">
                      <Binding ElementName="Questionctl" Path="CurrentTimer.Remaining.Minutes"/>
                      <Binding ElementName="Questionctl" Path="CurrentTimer.Remaining.Seconds"/>
                </MultiBinding> 
           </TextBlock.Text>
     </TextBlock>
     <TextBlock Text="{Binding ElementName=Questionctl,Path=CurrentTimer.Duration,StringFormat=HH:mm:ss}"/>
     <TextBlock Text="{Binding ElementName=Questionctl,Path=CurrentTimer.Elapsed,StringFormat=HH:mm:ss}"/>

计时器初始化如下:

dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan( 0 , 0, 1);
dispatcherTimer.Start();

如此简单,每一秒钟,它将在属性Elapsed中增加1秒:

CurrentTimer.Elapsed = CurrentTimer.Elapsed.AddSeconds(1);

更新您的myTimer的类定义以实现INotifyPropertyChanged如下所示:

public class myTimer : INotifyPropertyChanged
{
    private System.DateTime _duration;

    public System.DateTime Duration
    {
        get
        {
            return _duration;
        }
        set
        {
            _duration = value;
            RaisePropertyChanged("Duration");
            RaisePropertyChanged("Remaining");
        }
    }

    private DateTime _elapsed;

    public DateTime Elapsed
    {
        get { return _elapsed; }
        set
        {
            _elapsed = value;
            RaisePropertyChanged("Elapsed");
            RaisePropertyChanged("Remaining");
        }
    }

    public System.TimeSpan Remaining
    {
        get
        {
            return Duration.Subtract(new DateTime(Duration.Year, Duration.Month, Duration.Day, Elapsed.Hour, Elapsed.Minute, Elapsed.Second));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

如果那是您的实际问题而不是简化,那么您将遇到另一个问题。

仅当并且仅在调度计时器事件每隔一秒发生一次时,您的代码才起作用。 该api不能保证这一点。

如果改为在定时器触发时捕获系统时间,则即使事件触发之间的时间是零星的,它也会计算正确的时间。

暂无
暂无

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

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