I’m writing a WPF calculator app. In this application I have TextBox that get and set the input according to the OnWindowKeyDown event (input insert/validated and returned to the TextBox as the user type).
for example if the user type:
3-> validate == true--> print to TextBox '3'
y-> validate == false -> ignore
3-> validate == true --> print to TeextBox "33"
I want to get the input char by char (I have a State Pattern class that react to each char added), but I need to return the result as a string.
How can I transfer my data from and to the textBox, with the MVVM design?
It should be enough to bind a
Textproperty ofTextBoxtoViewModel‘s property.For example:
Bind
MyViewModelto aDataContextof yourForm,TextBoxor whatever… in short make it available to yourTextBoxin some way.Define a
Converterwhom methods will be invoked every timeTextproperty ofTextBoxset.After binding in
XAMLconsidering thatDataContextis an object of typeMyViewModelwhere
TextContenyConverterObjectis an object of typeTextContenyConverterdefined like a static resource.This just an example.
This is another good explantion on
ValueConverters.