如何绑定日期时间到MaskedTextBox中应用时,格式化字符串? [英] How to apply formatting string when binding DateTime to MaskedTextBox?
问题描述
我有一个 MaskedTextBox中
使用面膜00/00 / \\ 2 \\ 000
来限制输入的格式XX / XX / 20XX
,以及绑定到 MyBindingSource.SomeProperty的
类型文本
属性的DateTime
。
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.
如何申请的ToString(MMDDYYYY)
在的DateTime
对象转换为字符串,前值被绑定到文本
属性?
How can I apply ToString("MMddyyyy")
when converting the DateTime
object to a string, before the value is bound to the Text
property?
推荐答案
您可以使用绑定的的解析和的格式事件自己做转换,如<一看到href=\"http://stackoverflow.com/questions/982261/bind-nullable-datetime-to-maskedtextbox/986666#986666\">this回答
You can use the binding's Parse and Format events to do the conversion yourself, as seen in this answer
这篇关于如何绑定日期时间到MaskedTextBox中应用时,格式化字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!