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?
I don’t know if I’m not understanding your question, but the
Bindingclass handles conversions of basic types for you.TextBox.Textcan only ever show string/text value regardless of what it is bound to (int, double, DateTime, etc.). TheBindingis 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
TextBoxto only acceptDateTimevalues knowing that the user is going to enter them as aStringand theBindingwill convert it to the expectedDateTimevalue. 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?