1.引入命名空间
xmlns:sys="clr-namespace:System;assembly=System.Runtime"
2.显示时间代码
<StackPanel Orientation="Horizontal" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Left"> <TextBlock FontSize="15" DataContext="{x:Static sys:DateTime.Now}"> <Run Text="{Binding Now,Mode=OneWay,StringFormat={}{0:yyyy年MM月dd日 HH:mm:ss dddd},ConverterCulture=zh-CN}" Name="tb_datetime" Foreground="White"/> </TextBlock> </StackPanel>
3.添加触发器
<Window.Triggers> <EventTrigger RoutedEvent="UserControl.Loaded"> <BeginStoryboard> <Storyboard> <ObjectAnimationUsingKeyFrames Duration="0:0:2" Storyboard.TargetName="tb_datetime" Storyboard.TargetProperty="DataContext" RepeatBehavior="Forever"> <DiscreteObjectKeyFrame Value="{x:Static sys:DateTime.Now}" KeyTime="0:0:0"/> <DiscreteObjectKeyFrame Value="{x:Static sys:DateTime.Now}" KeyTime="0:0:1"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger> </Window.Triggers>
标签:触发器,System,实时,获取,前台,WPF From: https://www.cnblogs.com/SmallChen/p/18364233