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 inst开发者_JS百科ance 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?
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?
精彩评论