绑定到不与DispatcherTimer

时间:2016-02-29 18:13:35

标签: c# wpf binding 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代码位于DependencyProperty类型myTimer

后面
    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);

2 个答案:

答案 0 :(得分:2)

更新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));
        }
    }
}

答案 1 :(得分:0)

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

只有当且仅当调度计时器事件以一秒的间隔发生时,您的代码才有效。 api无法保证这一点。

如果您在计时器触发时捕获系统时间,它将计算正确的时间,即使事件发射之间的时间是零星的。