XAML
文件引入System
命名空间
<UserControl x:Class="UI.UCInformation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:UI"
mc:Ignorable="d"
d:DesignHeight="240" d:DesignWidth="400">
<!--其他代码-->
</UserControl>
- 增加
TextBox
控件并绑定Now
属性
<Grid Margin="10 25 10 10">
<TextBlock Text={Binding Source={x:Static sys:DateTime.Now}, StringFormat={}{0:yyyy年mm月dd日}}"/>
</Grid>
3 绑定后效果
- 完整
XAML
代码
<UserControl x:Class="UI.UCInformation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:UI"
mc:Ignorable="d"
xmlns:SAUtil="clr-namespace:SAUtilLib;assembly=SAUtilLib"
d:DesignHeight="240" d:DesignWidth="400">
<UserControl.Resources>
<sys:DateTime x:Key="DataTimeDataSource" d:IsDataSource="True"/>
</UserControl.Resources>
<Grid Margin="10 25 10 10" DataContext="{x:Static local:SysCfg.Default}">
<TextBlock Text={Binding Source={x:Static sys:DateTime.Now}, StringFormat={}{0:yyyy年mm月dd日}}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</UserControl>