样式
<Style x:Key="QuartzMSFont">
<Setter Property="TextElement.FontFamily" Value="Resources/#Quartz Regular"/>
</Style>
前端UI
<TextBlock x:Name="ColorTimer" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="68" Margin="5,10"
Background="Black" Foreground="Green" Style="{StaticResource QuartzMSFont}"/>
后台逻辑
private DispatcherTimer dispatcherTimer;
public MainWindow()
{
InitializeComponent();
this.DataContext = this;//数据绑定到当前 this
this.ColorTimer.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
//电子时钟
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
// 当间隔时间过去时发生的事件
dispatcherTimer.Tick += new EventHandler(ShowCurrentTime);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1);
dispatcherTimer.Start();
}
public void ShowCurrentTime(object sender, EventArgs e)
{
this.ColorTimer.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
解决方案
效果图
字体资源地址
“WPF 示例字体资源 ”