WPF文本绑定显示格式StringFormat设置-特殊格式时间日期和多数据绑定

WPF文本绑定显示格式StringFormat设置

特殊格式设置

在Textblock等文本控件中,我们经常要显示一些日期和时间,默认显示的日期,不是我们想要的,所以需要自定义格式,还有多个数据可能需要绑定到一个文本中,都可以通过设置StringFormat来实现

日期/时间

使用系统默认样式

xml 复制代码
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding DtNow, StringFormat={}{0:d}}" />
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding DtNow, StringFormat={}{0:D}}" />
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding DtNow, StringFormat={}{0:m}}" />
<TextBlock Grid.Row="3" Grid.Column="1" Text="{Binding DtNow, StringFormat={}{0:M}}" />
<TextBlock Grid.Row="4" Grid.Column="1" Text="{Binding DtNow, StringFormat={}{0:f}}" />
<TextBlock Grid.Row="5" Grid.Column="1" Text="{Binding DtNow, StringFormat={}{0:F}}" />
<TextBlock Grid.Row="6" Grid.Column="1" Text="{Binding DtNow, StringFormat={}{0:g}}" />
<TextBlock Grid.Row="7" Grid.Column="1" Text="{Binding DtNow, StringFormat={}{0:G}}" />
<TextBlock Grid.Row="8" Grid.Column="1" Text="{Binding DtNow, StringFormat={}{0:t}}" />
<TextBlock Grid.Row="9" Grid.Column="1" Text="{Binding DtNow, StringFormat={}{0:T}}" />

显示效果如下:

自定义格式:

xml 复制代码
<TextBox Text="{Binding DtNow, StringFormat={}{0:yyyy年MM月dd日}}" />
<TextBox Text="{Binding DtNow, StringFormat={}{0:yyyy-MM-dd}}" />
<TextBox Text="{Binding DtNow, StringFormat={}{0:yyyy-MM-dd HH:mm:ss}}" />
          

也可以简化直接写日期格式

xml 复制代码
  <TextBlock Text="{Binding DtNow,StringFormat='yyyy年MM月dd日'}"/>
  <TextBlock Text="{Binding DtNow,StringFormat='yyyy:MM:dd HH:mm:ss'}"/>

显示效果如下

绑定多个属性(多重绑定)

一个文本控件绑定多个属性合并显示,使用MultiBinding,在StringFormat中设置绑定格式{0}代表第一个属性,{1}代表第二个属性,以此类推

xml 复制代码
 <TextBlock>
     <TextBlock.Text>
         <MultiBinding StringFormat="数据计算:{0}/{1}={2}">
             <Binding Path="Value1" />
             <Binding Path="Value2" />
             <Binding Path="Result" />
         </MultiBinding>
     </TextBlock.Text>
 </TextBlock>

显示效果

多重绑定中的特殊字符

xml 复制代码
    \a  &#x07;  BEL
    \b  &#x08;  BS - Backspace
    \f  &#x0c;  FF - Formfeed
    \n  &#x0a;  LF, NL - Linefeed, New Line
    \r  &#x0d;  CR - Carriage return
    \t  &#x09;  HT - Tab
    \v  &#x0b;  VT - Vertical Tabelator 
        &gt;     >(右箭头)

示例:

xml 复制代码
<TextBlock>
     <TextBlock.Text>
         <MultiBinding StringFormat="数据计算:{0}/{1}--&gt;{2}">
             <Binding Path="Value1" />
             <Binding Path="Value2" />
             <Binding Path="Result" />
         </MultiBinding>
     </TextBlock.Text>
 </TextBlock>
    <!--

    -->

效果

相关推荐
BD_Marathon12 分钟前
消费者API
c#·linq
追逐时光者10 小时前
.NET 使用 MethodTimer 进行运行耗时统计提升代码的整洁性与可维护性!
后端·.net
CallZhang21013 小时前
Vision Master的C#脚本与opencv联合编程
opencv·计算机视觉·c#·视觉检测
AI视觉网奇14 小时前
kafka 冲突解决 kafka安装
c#·linq
hqwest14 小时前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局
萘柰奈15 小时前
Unity进阶--C#补充知识点--【Unity跨平台的原理】Mono与IL2CPP
unity·c#·游戏引擎
程序设计实验室15 小时前
StarBlog v1.3.0 新版本,一大波更新以及迁移服务器部署
c#·aspnetcore·starblog番外
淡海水15 小时前
【原理】Struct 和 Class 辨析
开发语言·c++·c#·struct·class
淡海水16 小时前
【原理】Unity GC 对比 C# GC
unity·c#·gc·垃圾回收
张人玉16 小时前
C#读取文件, IO 类属性及使用示例
microsoft·c#