У меня есть TextBlock, связанный с свойством с DependencyProperty. Когда DispatcherTimer изменяет это свойство, TextBlock не обновляется. Даже при отладке я вижу, что свойство обновляется, но TextBlock остается неизменным.Переплет для свойства, не работающего с DispatcherTimer
Детали: У меня есть класс:
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();
так просто, каждый второй, он добавит 1 секунду к свойству Прошедшее:
CurrentTimer.Elapsed = CurrentTimer.Elapsed.AddSeconds(1);