2012-10-27 25 views
1

我正在編寫一個WPF應用程序,顯示某個耗時任務的狀態。如何將一個變量傳遞給WPF綁定?

我有一個TextBlock綁定到任務的開始時間,並且我有另一個TextBlock用於顯示花費在所述任務上的時間量。

使用與轉換器的簡單綁定,我能夠得到第二個TextBlock正確顯示TimeSpan,但我對這個解決方案並不是太瘋狂。隨着時間的推移,我希望第二個TextBlock能夠更新TimeSpan。

例如,當用戶加載應用程序,第二個TextBlock中會說「五分鐘」,但15分鐘後,它仍然會說「五分鐘」,這是一種誤導。

我能找到這個解決方案(Binding to DateTime.Now. Update the value),這是痛苦地接近我想要的東西,但也不能令人信服。

是否有任何方法可以將任務的開始時間傳遞給此Ticker類並自動更新文本?

編輯:這是我到目前爲止有:
C#:
公共類股票代碼:INotifyPropertyChanged的 { 公共靜態的DateTime開始時間{獲得;組; }

public Ticker() 
    { 
     Timer timer = new Timer(); 
     timer.Interval = 1000; 
     timer.Elapsed += timer_Elapsed; 
     timer.Start(); 
    } 

    public string TimeDifference 
    { 
     get 
     { 
      string timetaken = ""; 
      try 
      { 
       TimeSpan ts = DateTime.Now - StartTime; 
       timetaken = (StartTime == default(DateTime)) ? "StartTime not set!" : ((ts.Days * 24) + ts.Hours).ToString("N0") + " hours, " + ts.Minutes + " minutes, " + ts.Seconds + " seconds"; 
      } 
      catch (Exception) { } 

      return timetaken; 
     } 
    } 

    private void timer_Elapsed(object sender, ElapsedEventArgs e) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs("TimeDifference")); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

XAML:
<StackPanel Grid.Row="2" Grid.Column="0" Orientation="Horizontal">
<TextBlock Text="Dataset started on:"/>
<TextBlock Name="starttimeLabel" Text="10/23/2012 4:42:26 PM"/>
</StackPanel>
<StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal">
<TextBlock Text="Time taken:"/>
<TextBlock Name="timespanLabel" Text="{Binding Source={StaticResource ticker}, Path=TimeDifference, Mode=OneWay}"/>
</StackPanel>

最後,在我的狀態面板的Loaded事件,我有以下幾點:
Ticker.StartTime = Convert.ToDateTime(starttimeLabel.Text);

我的惡劣格式化道歉,但我試圖按照指示無濟於事(插入4位代碼塊,然後使用<code></code>等試過)

+0

你的任務是什麼意思? 你能添加一些代碼嗎? –

+0

在這種情況下,「任務」的定義不會影響答案。但是,任務涉及在一組計算機上生成數百萬張圖像,因此可能需要幾天的時間。這就是爲什麼我不想顯示任務已經處理的時間量。 –

回答

0

使用DispatcherTimer「爲」倒計時嘗試:

XAML:

<Window x:Class="CountDown.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
    <TextBlock x:Name="initialTime" Width="50" Height="20" Margin="165,66,288,224"></TextBlock> 
    <TextBlock x:Name="txtCountDown" Width="50" Height="20" Margin="236,66,217,225"></TextBlock> 
    </Grid> 
</Window> 

C#:

namespace CountDown 
    { 
     /// <summary> 
     /// Interaction logic for MainWindow.xaml 
     /// </summary> 
     public partial class MainWindow : Window 
     { 
      private DispatcherTimer cDown; 

      public MainWindow() 
      { 
       InitializeComponent(); 
       initialTime.Text = DateTime.Now.ToString("HH:mm:ss"); 
       TimeSpan timeToDie = new TimeSpan(0, 0, 10); //Set the time "when the users load" 
       txtCountDown.Text = timeToDie.ToString(); 

       cDown = new DispatcherTimer(); 
       cDown.Tick += new EventHandler(cDown_Tick); 
       cDown.Interval = new TimeSpan(0, 0, 1); 
       cDown.Start(); 
      } 


      private void cDown_Tick(object sender, EventArgs e) 
      { 
       TimeSpan? dt = null; 
       try 
       { 
        dt = TimeSpan.Parse(txtCountDown.Text); 
        if (dt != null && dt.Value.TotalSeconds > 0 ) 
        { 
         txtCountDown.Text = dt.Value.Add(new TimeSpan(0,0,-1)).ToString(); 
        } 
        else 
        { 
         cDown.Stop(); 
        } 
       } 
       catch (Exception ex) 
       { 
        cDown.Stop(); 
        Console.WriteLine(ex.Message); 
       } 


      } 

     } 
    } 
+0

這是接近我正在尋找,但不完全。我希望這可以通過綁定在標記中完成。 –

相關問題