WPF 前台触发器实时获取当前时间
1.引入命名空间
1 | xmlns:sys= "clr-namespace:System;assembly=System.Runtime" |
2.显示时间代码
1 2 3 4 5 | <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.添加触发器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <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> |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步