Binding a decimal to a UITextField does not let you put a "." in, as you type "1.", the push to source strips it out. I get why it happens, MakeSafeValue converts it to a decimal, which gets read back out without the ".", overwriting the Text entered. This seems like a keyup vs onblur issue, which a lot of binding frameworks let you override.
I know I could bind a string on the view model instead, but handling the EditingDidEnd instead of EditingChanged seems better. That way I could intercept ShouldEndEditing to check validity.
Where would I register my binding to override the MvvmCross implementation? I tried adding in my Setup.FillTargetFactories, but it is called before MVXTouchBindingBuilder.FillTargetFactories where the MvvmCross one is.
I tried a different property name, in FillTargetFactories I have:
registry.RegisterPropertyInfoBindingFactory(typeof (UITextFieldTextDidEndTargetBinding), typeof (UITextField), "DidEndText");
but it does not work when I bind.
set.Bind(textField).For("DidEndText").To(vm => vm.GMoney);
looks like MvxPropertyInfoTargetBindingFactory.CreateBinding does not work if the target does not have that property = makes sense. Does this leave me with starting at MvxTargetBinding and creating a custom binding? I hate to recreate most of the code in MvxPropertyInfoTargetBinding.
So, I guess the question(s) are...
- what is the syntax/location to override the default UITextField binding with mine?
- how can I specify a different binding for Text with the Bind Syntax?
Thanks in advance.
If you want to simply replace the existing binding, then you can do this via the
RegisterPropertyInfoBindingFactory
extension method.MvvmCross operates a simple 'last registered wins' system.
The easiest place your registration in order that happens last is to place it at the end of
InitializeLastChance
in yourSetup
:There's some more info on order of initialization/setup in https://github.com/slodge/MvvmCross/wiki/Customising-using-App-and-Setup (work in progress).
The line:
doesn't work because it tries to use a property called
DidEndText
- which doesn't exist.Two possible ways around this are:
1 Subclass
UITextField
to provide a real propertyAn example of
UITextField
subclassing is shown in the N=33 video2 Or use a non-propertyInfo-based binding
For known properties and event pairs this is pretty simple to do:
This special binding can be registered using
RegisterCustomBindingFactory
After which bindings like:
will work.
As well as your 'use a string in the ViewModel' suggestion, another possible workaround for this double-text conversion, might be to use a two-way value converter. If your UI is handling money, this could insist on displaying decimal places for all values.