2011-11-28 80 views
2

我想獲得一個textblock來顯示我的秒錶的運行時間,但我不確定如何完成此操作。在TextBlock中運行秒錶

我試過使用綁定到秒錶的過去的方法,但這不顯示任何東西。

<TextBlock Grid.Row="1" Height="23" HorizontalAlignment="Right" Margin="0,0,398,0" Name="textBlockElapsed" Text="{Binding Path=watch.Elapsed, Mode=OneWay}" VerticalAlignment="Top" /> 

    Stopwatch watch = new Stopwatch(); 
+0

您使用一個視圖模型?你對你的數據上下文有什麼注意? – Mark

回答

2

嘗試下面的示例代碼:MainWindow.xaml.cs

public partial class MainWindow : Window 
{ 

    DispatcherTimer dt = new DispatcherTimer(); 
    Stopwatch stopWatch = new Stopwatch(); 
    string currentTime = string.Empty; 
    public MainWindow() 
    { 
     InitializeComponent(); 
     SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvents_SessionEnding); 
     SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch); 
     dt.Tick += new EventHandler(dt_Tick); 
     dt.Interval = new TimeSpan(0, 0, 0, 0, 1); 
    } 

    void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e) 
    { 
     if (Environment.HasShutdownStarted) 
     { 
      MessageBox.Show("Total amount of time the system logged on:" + ClockTextBlock.Text); 
     } 

    } 

    void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e) 
    { 
     switch (e.Reason) 
     { 
      // ... 
      case SessionSwitchReason.SessionLock: 
       if (stopWatch.IsRunning) 
        stopWatch.Stop();      
       break; 
      case SessionSwitchReason.SessionUnlock: 
       stopWatch.Start(); 
       dt.Start(); 
       break; 
      case SessionSwitchReason.SessionLogoff: 
       MessageBox.Show("Total amount of time the system logged on:" + ClockTextBlock.Text); 
       break; 
      // ... 
     } 
    } 

    void dt_Tick(object sender, EventArgs e) 
    { 
     if (stopWatch.IsRunning) 
     { 
      TimeSpan ts = stopWatch.Elapsed; 
      currentTime = String.Format("{0:00}:{1:00}:{2:00}", 
       ts.Hours, ts.Minutes, ts.Seconds); 
      ClockTextBlock.Text = currentTime; 
     } 
    } 
    private void StartButton_Click(object sender, RoutedEventArgs e) 
    { 
     stopWatch.Start(); 
     dt.Start(); 
    } 

    private void StopButton_Click(object sender, RoutedEventArgs e) 
    { 
     if (stopWatch.IsRunning) 
      stopWatch.Stop(); 
    } 

    private void ResetButton_Click(object sender, RoutedEventArgs e) 
    { 
     stopWatch.Reset(); 
     stopWatch.Start(); 
    } 
} 

MainWindow.xaml:

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition />    
     <ColumnDefinition /> 
     <ColumnDefinition /> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="0.3*"/> 
     <RowDefinition Height="0.2*"/> 

    </Grid.RowDefinitions> 
    <TextBlock Name="ClockTextBlock" 
      TextAlignment="Center" 
      VerticalAlignment="Center" 
      FontSize="35" Foreground="Red" 
      Grid.ColumnSpan="4" 
      Grid.Row="0" /> 
    <Button Content="Start" 
     Name="StartButton" 
     Grid.Row="1" 
     Grid.Column="0" 
     Width="60" Height="35" 
     Click="StartButton_Click" />   
    <Button Content="Stop" 
     Name="StopButton" 
     Grid.Row="1" 
     Grid.Column="1" 
     Width="60" Height="35" 
     Click="StopButton_Click" /> 
    <Button Content="Reset" 
     Name="ResetButton" 
     Grid.Row="1" 
     Grid.Column="3" 
     Width="60" Height="35" 
     Click="ResetButton_Click" />  
</Grid> 
1

下面是完整的例子。

MainWindow.xaml.cs:

public partial class MainWindow : Window, INotifyPropertyChanged 
{ 
    public MainWindow() 
    { 
     this.Stopwatch = new Stopwatch(); 
     this.Stopwatch.Start(); 
     this._timer = new Timer(
      new TimerCallback((s) => this.FirePropertyChanged(this, new PropertyChangedEventArgs("Stopwatch"))), 
      null, 1000, 1000); 

     InitializeComponent(); 
    } 

    private void FirePropertyChanged(object sender, PropertyChangedEventArgs e) 
    { 
     if (this.PropertyChanged != null) 
     { 
      this.PropertyChanged(sender, e); 
     } 
    } 

    private Timer _timer; 

    public Stopwatch Stopwatch { get; set; } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

MainWindow.xaml:

<Window x:Class="WpfApplication1.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 Text="{Binding Path=Stopwatch.Elapsed, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" /> 
    </Grid> 
</Window> 
+0

我已經在使用C#Stopwatch類了,我想我可以將它命名爲Elapsed,並將getter設置爲stopwatch.elapsed,這樣可以工作嗎? –

+0

試過這個,但TextBlock仍然沒有顯示任何想法? –

2

Stopwatch不具有Elapsed方法。它具有Elapsed屬性,但只有在查詢它時纔會更新。

Lolo's answer顯示如何將Elapsed屬性綁定到您的文本字段。如果您希望顯示不斷更新,您需要創建一個timer,這將觸發該字段的更新。

+0

我的意思是財產,但說方法,哎呀,哈哈。謝謝您的幫助! –