c# - 如何在 Wpf 状态栏上显示当前日期和时间
问题描述
我想知道如何在 WPF 状态栏上显示当前日期和时间。
我知道这是一个太基本的问题,但我是 .net WPF 的新手,我知道这可以在 Form 应用程序中轻松完成。
提前致谢。
婷
解决方案
创建一个 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
每秒更新 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();
}
还有很多使用Template
和Style
属性自定义 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>
推荐阅读
- php - 如何绕过 PHP 连接中的错误 111 MySQL?
- r - 是否有一个 R 函数可以在某个字符串之后移动数据?
- python - azure keyvault 和 azure identity 共存
- html - Namecheap DNS & Xampp Webserver - 重定向
- unit-testing - Redux saga 选择器单元测试问题
- terraform - 带有可选值的 Yamldecode
- java - 当您更改 xml 文件中的属性时,会调用哪个自定义视图的构造函数?
- php - 通过 nginx/laravel API 调用登录尝试失败
- r - 如何修复在 Shiny 中未弹出 esquisserUI 的过滤器选项?
- javascript - 下一个 js:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义