簡體   English   中英

如何在 Wpf 狀態欄上顯示當前日期和時間

[英]How to display current date and time on Wpf statusbar

我想知道如何在 WPF 狀態欄上顯示當前日期和時間。

我知道這是一個太基本的問題,但我是 .net WPF 的新手,我知道這可以在表單應用程序中輕松完成。

提前致謝。

創建 wpf 項目。 在您的MainWindow.xaml添加StatusBar並處理 window 的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每秒更新 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>

你可以這樣做,使用 wpf animation 和綁定,並且沒有背景代碼:)

<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>
</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">
    <Window.Resources>
        <FrameworkElement x:Key="time" Tag="{x:Static system:DateTime.Now}" />

        <TransformGroup x:Key="transformHour">
            <TranslateTransform X="{Binding Source={StaticResource time},Path=Tag.Hour}"
                                Y="{Binding Source={StaticResource time},Path=Tag.Minute}" />
            <MatrixTransform Matrix="30 0 0.5 0 0 0" />
        </TransformGroup>

        <TransformGroup x:Key="transformMinute">
            <TranslateTransform X="{Binding Source={StaticResource time},Path=Tag.Minute}"
                                Y="{Binding Source={StaticResource time},Path=Tag.Second}" />
            <MatrixTransform Matrix="6 0 0.1 0 0 0" />
        </TransformGroup>

        <TransformGroup x:Key="transformSecond">
            <TranslateTransform X="{Binding Source={StaticResource time},Path=Tag.Second}" />
            <MatrixTransform Matrix="6 0 0 0 0 0" />
        </TransformGroup>

        <Style TargetType="{x:Type Path}">
            <Setter Property="Stroke"
                    Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
            <Setter Property="StrokeThickness" Value="3" />
            <Setter Property="StrokeDashCap" Value="Triangle" />
        </Style>

    </Window.Resources>

    <Viewbox>
        <Canvas Width="200" Height="200">
            <Canvas.RenderTransform>
                <TranslateTransform X="100" Y="100" />
            </Canvas.RenderTransform>

            <Path Data="M 0 -90 A 90 90 0 1 1 -0.01 -90"
                StrokeDashArray="0 3.14157" />

            <Path Data="M 0 -90 A 90 90 0 1 1 -0.01 -90"
                StrokeDashArray="0 7.854"
                StrokeThickness="6" />

            <Border Background="LightBlue" Width="10" Height="80" RenderTransformOrigin="0.5 0">
                <Border.RenderTransform>
                    <TransformGroup>
                        <RotateTransform x:Name="bor_Second"
                                        Angle="{Binding Source={StaticResource transformSecond},Path=Value.OffsetX}" />
                        <RotateTransform Angle="180" />
                    </TransformGroup>
                </Border.RenderTransform>
            </Border>

            <Border Background="LightGreen" Width="10" Height="60" RenderTransformOrigin="0.5 0">
                <Border.RenderTransform>
                    <TransformGroup>
                        <RotateTransform x:Name="bor_Minute"
                                        Angle="{Binding Source={StaticResource transformMinute},Path=Value.OffsetX}" />
                        <RotateTransform Angle="180" />
                    </TransformGroup>
                </Border.RenderTransform>
            </Border>

            <Border Background="LightGray" Width="10" Height="40" RenderTransformOrigin="0.5 0">
                <Border.RenderTransform>
                    <TransformGroup>
                        <RotateTransform x:Name="bor_Hour"
                                        Angle="{Binding Source={StaticResource transformHour},Path=Value.OffsetX}" />
                        <RotateTransform Angle="180" />
                    </TransformGroup>
                </Border.RenderTransform>
            </Border>

        </Canvas>
    </Viewbox>
    <Window.Triggers>
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="bor_Hour"
                                    Storyboard.TargetProperty="Angle"
                                    IsAdditive="True"
                                    Duration="12:0:0"
                                    From="0" To="360"
                                    RepeatBehavior="Forever" />
                    <DoubleAnimation Storyboard.TargetName="bor_Minute"
                                    Storyboard.TargetProperty="Angle"
                                    IsAdditive="True"
                                    Duration="1:0:0"
                                    From="0" To="360"
                                    RepeatBehavior="Forever" />
                    <DoubleAnimation Storyboard.TargetName="bor_Second"
                                    Storyboard.TargetProperty="Angle"
                                    IsAdditive="True"
                                    Duration="0:1:0"
                                    From="0"
                                    To="360"
                                    RepeatBehavior="Forever" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Window.Triggers>
</Window>

僅使用 wpf 中的 xaml 代碼創建時鍾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM