将DateTime绑定到MaskedTextBox时如何应用格式化字符串? [英] How to apply formatting string when binding DateTime to MaskedTextBox?

查看:151
本文介绍了将DateTime绑定到MaskedTextBox时如何应用格式化字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MaskedTextBox 使用掩码00/00 / \\000来限制输入格式为 XX / XX / 20XX ,其中 Text 属性绑定到 MyBindingSource DateTime 类型的.SomeProperty

I have a MaskedTextBox using the mask "00/00/\2\000" to restrict input to a format of XX/XX/20XX, with the Text property bound to MyBindingSource.SomeProperty of type DateTime.

最近,单位数月份或日期的值最近开始显示不正确。我期望在数据绑定中的某个时候调用 ToString()方法,而且我相信这个调用并不是以零为单位填满月/日。

Recently, values with a single-digit month or day recently started displaying incorrectly. I expect that the ToString() method is being called on the value at some point in data binding, and I believe the call is not padding month / day with zeroes.

我尝试将 MaskedTextBox.Text 属性的格式类型设置为 DateTime 在高级数据绑定属性中,但没有帮助。

I tried setting the format type of the MaskedTextBox.Text property to DateTime in the advanced data binding properties, but it didn't help.

当转换 DateTime 时,如何申请 ToString(MMddyyyy) code>对象到一个字符串之前,该值绑定到 Text 属性?

How can I apply ToString("MMddyyyy") when converting the DateTime object to a string, before the value is bound to the Text property?

推荐答案

您可以使用绑定的解析格式事件自己进行转换,如此答案

You can use the binding's Parse and Format events to do the conversion yourself, as seen in this answer

这篇关于将DateTime绑定到MaskedTextBox时如何应用格式化字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆