WPF 文本框绑定到数字或日期时间字段/属性
我见过许多关于“使文本框仅允许数字输入”的参考...没问题,我知道如何做到这一点。但是,典型的绑定仍然是 TextBox 的“Text”(TextProperty 依赖项),它是一个字符串值。
如果输入的值被强制为数字(例如...整数,或双精度,浮点数用于最终存储),那么您将如何进行正确的双向绑定说
TextBox.MyInteger
TextBox.MyDouble
TextBox.MyFloat
TextBox.MyDateTime
所以,如果我有一个具有(例如)的类,
public class MyRecord
{
public int IntegerOnly { get; set; }
public double DoubleOnly { get; set; }
public DateTime SomeDate { get; set; }
}
并且我在窗口上有一个用于输入的文本框,具有适用的行为/过滤器,仅允许在文本框中输入数字(和小数点)值,向用户的显示是通过显示的“文本”值。
因此,我想要一个“MyRecord.IntegerOnly”的实例来获取推回(到数据库)和来回(到用户查看/编辑的视图)的数值。
由于 C# 中的所有内容都是类型转换的,因此我没有看到任何从文本到数字的“隐含”或“转换”值。
同样,用于 DATE 或 DATETIME TextBox 控件的数据输入。没有隐含/转换值...
我该如何/应该继续此操作?
I've seen many references of make a TextBox only allow numeric entry... no problem, I understand how to do that. However, the typical binding is still to the "Text" (TextProperty dependency) of the TextBox which is a string value.
If the value being entered is being forced to that of a numeric (say... either integer, or double, float for final storage), how would you go about having the correct 2-way binding to say
TextBox.MyInteger
TextBox.MyDouble
TextBox.MyFloat
TextBox.MyDateTime
So, if I had a class that had (for example)
public class MyRecord
{
public int IntegerOnly { get; set; }
public double DoubleOnly { get; set; }
public DateTime SomeDate { get; set; }
}
And I have a TextBox on a window for entry, with applicable behaviors/filters to ONLY allow numeric (and decimal point) value to be entered into the TextBox, the display to the user is via the shown "Text" value.
So, I want an instance of the "MyRecord.IntegerOnly" to get the numeric value pushed back (to the database) and forth (to the view for the user to see/edit).
Since everything in C# is type-cast, I don't see any "implied" or "conversion" value from the text to numeric.
Similarly, for doing data entry of a DATE or DATETIME TextBox control. No implied / converted value...
How can / should I proceed with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道我是否不理解您的问题,但是
Binding
类为您处理基本类型的转换。TextBox.Text
只能显示字符串/文本值,无论它绑定到什么(int、double、DateTime 等)。Binding
负责在目标和源之间来回转换值,即Int32 -> String
(目标)和String -> Int32(返回源代码)。
您可以非常轻松地指定
TextBox
只接受DateTime
值,因为知道用户将以String
的形式输入它们,并且Binding
会将其转换为预期的DateTime
值。当您需要转换Binding
未自动处理的值时,您需要提供自己的IValueConverter
。这能回答你的问题还是我错过了重点?
I don't know if I'm not understanding your question, but the
Binding
class handles conversions of basic types for you.TextBox.Text
can only ever show string/text value regardless of what it is bound to (int, double, DateTime, etc.). TheBinding
is responsible for converting values back-and-forth between target and source, i.e.,Int32 -> String
(to target) andString -> Int32
(back to source).You can very easily designate a
TextBox
to only acceptDateTime
values knowing that the user is going to enter them as aString
and theBinding
will convert it to the expectedDateTime
value. When you need to convert values not automatically handled by theBinding
, you need to provide your ownIValueConverter
.Does that answer your question or have I missed the point?