首页 > 解决方案 > 如何在 Wpf 状态栏上显示当前日期和时间

问题描述

我想知道如何在 WPF 状态栏上显示当前日期和时间。

我知道这是一个太基本的问题,但我是 .net WPF 的新手,我知道这可以在 Form 应用程序中轻松完成。

提前致谢。

标签: c#wpfdatestatusbar

解决方案


创建一个 wpf 项目。在您MainWindow.xaml添加StatusBar和处理Loaded窗口的事件。

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="WpfApp1.MainWindow"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <StatusBar Grid.Row="1">
            <TextBlock x:FieldModifier="private" x:Name="myDateTime"/>
        </StatusBar>
    </Grid>
</Window>

MainWindow.xaml.cs添加以下命名空间(如果它们不存在):

using System;
using System.Windows;
using System.Windows.Threading;

Loadedenevt 处理程序中,您可以使用DispatcherTimer每秒更新 textblock 文本属性:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
   DispatcherTimer timer = new DispatcherTimer(TimeSpan.FromSeconds(1), DispatcherPriority.Normal, (object s, EventArgs ev) =>
   {
      this.myDateTime.Text = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss");
   }, this.Dispatcher);
   timer.Start();
}

还有很多使用TemplateStyle属性自定义 wpf 控件的示例。只是搜索它。

还有很多。

您也可以实现您的自定义WPFTimer. 这个简单的实现让你有个想法。

public class WPFTimer : TextBlock
{
   #region static

   public static readonly DependencyProperty IntervalProperty = DependencyProperty.Register("Interval", typeof(TimeSpan), typeof(WPFTimer), new PropertyMetadata(TimeSpan.FromSeconds(1), IntervalChangedCallback));
   public static readonly DependencyProperty IsRunningProperty = DependencyProperty.Register("IsRunning", typeof(bool), typeof(WPFTimer), new PropertyMetadata(false, IsRunningChangedCallback));

   private static void IntervalChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
   {
      WPFTimer wpfTimer = (WPFTimer)d;
      wpfTimer.timer.Interval = (TimeSpan)e.NewValue;
   }

   private static void IsRunningChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
   {
      WPFTimer wpfTimer = (WPFTimer)d;
      wpfTimer.timer.IsEnabled = (bool)e.NewValue;
   }

   #endregion

   private readonly DispatcherTimer timer;

   [Category("Common")]
   public TimeSpan Interval
   {
      get
      {
         return (TimeSpan)this.GetValue(IntervalProperty);
      }
      set
      {
         this.SetValue(IntervalProperty, value);
      }
   }

   [Category("Common")]
   public bool IsRunning
   {
      get
      {
         return (bool)this.GetValue(IsRunningProperty);
      }
      set
      {
         this.SetValue(IsRunningProperty, value);
      }
   }

   public WPFTimer()
   {
      this.timer = new DispatcherTimer(this.Interval, DispatcherPriority.Normal,this.Timer_Tick ,this.Dispatcher);
      this.timer.IsEnabled = false;
   }

   private void Timer_Tick(object sender, EventArgs e)
   {
      this.SetValue(TextProperty, DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss"));
   }
}

现在你有了一个可以在设计器中使用的控件。

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApp1"
        x:Class="WpfApp1.MainWindow"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <StatusBar Grid.Row="1">
            <local:WPFTimer IsRunning="True"/>
        </StatusBar>
    </Grid>
</Window>

推荐阅读