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>
    <!--

    -->

效果

相关推荐
Aevget39 分钟前
界面控件DevExpress WPF v25.1新版亮点:AI功能的全面升级
c#·.net·wpf·界面控件·devexpress·ui开发
Archy_Wang_11 小时前
Hangfire 入门与实战:在 .NET Core 中实现可靠后台任务处理
c#·.netcore
爱编程的鱼4 小时前
想学编程作为今后的工作技能,学哪种语言适用性更强?
开发语言·算法·c#·bug
清风与日月5 小时前
c#事件委托示例
开发语言·c#
用户3721574261355 小时前
C# 实现在 Excel 中高效生成和操作表格
c#
csdn_aspnet6 小时前
C# 金字塔体积计算程序(Program for volume of Pyramid)
c#
William_cl6 小时前
C# ASP.NET MVC Model 分类:数据传输对象(DTO)—— 跨层传数的 “精简快递“
c#·asp.net·mvc
zwjapple7 小时前
Kafka 从入门到精通完整指南
c#·linq
唐青枫9 小时前
C#.NET SemaphoreSlim 深入解析:轻量级异步锁与并发控制
c#·.net
我是苏苏12 小时前
C#高级:程序查询写法性能优化提升策略(附带Gzip算法示例)
开发语言·算法·c#