2016-05-16 63 views
1

我有一個帶倒數字符串的List,我想每隔一秒用一個DispatcherTimer更新它。List <>在DispatcherTimer中沒有更新

初始化(的窗口上的負荷運行

tasks.Add(new Tasks() 
{ 
    title = "Task 1", 
    date = "14:30 17 Martie 2016", 
    countdown = "1", 
    timer = new TimeSpan(0, 15, 32) 
}); 
tasks.Add(new Tasks() 
{ 
    title = "Task 2", 
    date = "14:30 17 Martie 2016", 
    countdown = "2", 
    timer = new TimeSpan(1, 10, 52) 
}); 
listViewTasks.ItemsSource = tasks; 

initCountdown(); 

public class Tasks 
{ 
public string title { get; set; } 
public string date { get; set; } 
public string countdown { get; set; } 
public TimeSpan timer { get; set; } 
} 

DispatcherTimer

public void initCountdown() 
     { 
string item = tasks[0].title; 
_time = tasks[0].timer; 
_timer = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate 
{ 
    tasks[0].countdown = _time.ToString("c"); //this does not update 
    if (_time == TimeSpan.Zero) _timer.Stop(); 
    _time = _time.Add(TimeSpan.FromSeconds(-1)); 
}, Application.Current.Dispatcher); 

_timer.Start(); 
} 

XAML

<ListView Margin="0,30,0,0" Name="listViewTasks"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="1*"/> 
        <ColumnDefinition Width="1*"/> 
       </Grid.ColumnDefinitions> 
       <StackPanel HorizontalAlignment="Left" Margin="0,0,50,0"> 
        <TextBlock Text="{Binding title}" Cursor="Hand"/> 
        <TextBlock Text="{Binding date}" /> 
       </StackPanel> 
       <StackPanel HorizontalAlignment="Right" Grid.Column="1"> 
        <TextBlock Text="{Binding countdown}" /> 
       </StackPanel> 
      </Grid> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

從調度員外我可以更新它,但從裏面不工作。

也不例外,用戶界面不會更新

我想更新列表視圖,以顯示我從我加入的時間跨度倒計時。

+0

你有例外嗎?還是隻是沒有更新? – Zack

+0

這是什麼意思「不起作用」?它從未達到零,或者ui不更新? – Liero

+0

@Zack也不例外 – user2252671

回答

3

您必須實現INotifyPropertyChanged接口在你的任務類並調用PropertyChanged事件時,倒計時屬性更改

+0

謝謝你正是我所需要的 – user2252671

1

更改Tasks類實現INotifyPropertyChanged接口。倒計時的值正在更新,用戶界面並沒有得到它的新值的通知。

public class Tasks : INotifyPropertyChanged 
{ 
    private string _title; 
    public string title 
    { 
     get { return _title; } 
     set { _title = value; OnPropertyChanged("title"); } 
    } 

    private string _date; 
    public string date 
    { 
     get { return _date; } 
     set { _date = value; OnPropertyChanged("date"); } 
    } 
    private string _countdown; 
    public string countdown 
    { 
     get { return _countdown; } 
     set { _countdown = value; OnPropertyChanged("countdown"); } 
    } 

    private TimeSpan _timer; 
    public TimeSpan timer 
    { 
     get { return _timer; } 
     set { _timer = value; OnPropertyChanged("timer"); } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    public void OnPropertyChanged(string propName) 
    { 
     var e = PropertyChanged; 
     if (e != null) 
     { 
      e.Invoke(this, new PropertyChangedEventArgs(propName)); 
     } 
    } 
} 
相關問題