当前位置: 首页 > news >正文

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

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

  • 特殊格式设置
  • 日期/时间
    • 使用系统默认样式
    • 自定义格式:
  • 绑定多个属性(多重绑定)
    • 多重绑定中的特殊字符
    • 示例:

特殊格式设置

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

日期/时间

使用系统默认样式

<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}}" />

显示效果如下:
在这里插入图片描述

自定义格式:

<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}}" />

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

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

显示效果如下
在这里插入图片描述

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

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

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

显示效果
在这里插入图片描述

多重绑定中的特殊字符

    \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;     >(右箭头)

示例:

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

效果
在这里插入图片描述

http://www.lryc.cn/news/382266.html

相关文章:

  • Java包介绍
  • 【2024.6.21】今日科技时事:科技前沿大事件
  • LeetCode:经典题之1491、896 题解与延伸
  • 2024三掌柜赠书活动第二十五期:Rust 游戏开发实战
  • 基于Java蛋糕甜品商城系统设计和实现(源码+LW+调试文档+讲解等)
  • Tomcat get请求传数组集合参数
  • 信息学奥赛初赛天天练-34-CSP-J2021完善程序-按位异或、模拟算法、数组模拟环、约瑟夫问题应用
  • 【计算机视觉】人脸算法之图像处理基础知识(六)
  • 仓颉编程语言入门
  • 在前端项目中,如何处理错误和异常?
  • Ubuntu系统下修改网卡IP地址
  • Scrapy如何对爬虫数据进行清洗和处理?
  • Linux:基础IO(三.软硬链接、动态库和静态库、动精态库的制作和加载)
  • 低价可转债崩盘,发生了什么?
  • 【面试题】马上金九银十了,简历该准备起来了,面试题你准备好了吗 ?浅谈 JS 浅拷贝和深拷贝
  • 最新OPPO 真我手机 一加手机 使用adb命令永久关闭系统更新教程
  • OnlyOffice:现代办公的最佳选择
  • 【收藏】2024年必备相图数据库资源集锦!
  • Zookeeper 二、Zookeeper环境搭建
  • Web3 学习
  • Grafana+Prometheus(InfluxDB)+Jmeter使用Nginx代理搭建可视化性能测试监控平台
  • web学习笔记(六十六)项目总结
  • 红队内网攻防渗透:内网渗透之内网对抗:横向移动篇域控系统提权NetLogonADCSPACKDC永恒之蓝CVE漏洞
  • VMware Workstation安装Windows Server2019系统详细操作步骤
  • HTML5【新特性总结】
  • 【面试题】面试官:判断图是否有环?_数据结构复试问题 有向图是否有环
  • 办理北京公司注册地址异常变更要求和流程
  • 当你在浏览器输入一个地址
  • JSP基础知识概述
  • 国产编程—— 仓颉