如何在Wpf状态栏上显示当前日期和时间

问题描述 投票:-2回答:3

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

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

提前感谢。

Ting

c# wpf date statusbar
3个回答
1
投票

创建一个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;

并且在Loaded enevt处理程序中,您可以每秒使用DispatcherTimer更新文本块文本属性:

DispatcherTimer

[还有很多使用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(); } Template属性定制wpf控件的示例。只是搜索它。

还有更多。

此外,您也可以实现自定义The WPF StatusBar control。这个简单的实现使您对此有所了解。

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"));
   }
}

0
投票

您可以使用wpf动画和绑定来完成此操作,并且没有背景代码:)

<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>

<Window x:Class="WpfApp6.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:system="clr-namespace:System;assembly=System.Runtime" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.Resources> <!--Set x: share to get the latest every time--> <system:DateTime x:Key="DateTime" x:Shared="False" /> <Storyboard x:Key="Storyboard"> <!--Use keyframe animation to update datetime --> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="DataContext" Duration="0:0:1" RepeatBehavior="Forever" AutoReverse="False"> <DiscreteObjectKeyFrame KeyTime="50%" Value="{StaticResource DateTime}" /> </ObjectAnimationUsingKeyFrames> </Storyboard> </Grid.Resources> <!--Get datetime from DataContext--> <TextBlock Text="{Binding RelativeSource={RelativeSource Self},Path=DataContext.Now}" DataContext="{StaticResource DateTime}"> <TextBlock.Triggers> <EventTrigger RoutedEvent="Loaded"> <BeginStoryboard Storyboard="{StaticResource Storyboard}" /> </EventTrigger> </TextBlock.Triggers> </TextBlock> </Grid>


© www.soinside.com 2019 - 2024. All rights reserved.