WPF 文本框绑定到数字或日期时间字段/属性

发布于 2024-12-08 11:15:22 字数 751 浏览 5 评论 0原文

我见过许多关于“使文本框仅允许数字输入”的参考...没问题,我知道如何做到这一点。但是,典型的绑定仍然是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给我一枪 2024-12-15 11:15:22

我不知道我是否不理解您的问题,但是 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.). The Binding is responsible for converting values back-and-forth between target and source, i.e., Int32 -> String (to target) and String -> Int32 (back to source).

You can very easily designate a TextBox to only accept DateTime values knowing that the user is going to enter them as a String and the Binding will convert it to the expected DateTime value. When you need to convert values not automatically handled by the Binding, you need to provide your own IValueConverter.

Does that answer your question or have I missed the point?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文