sialcasa / mvvmFX

an Application Framework for implementing the MVVM Pattern with JavaFX
Apache License 2.0
491 stars 105 forks source link

when bind model to TextField ,the textfield cant edit? #574

Closed forTribeforXuanmo closed 5 years ago

forTribeforXuanmo commented 5 years ago

when i bind model data to TextField ,the textfield cant edit , because the data mode Initialized in the java code, i cant edit the input textfield , in the beginning ,i think it like js framework "vue" can bind view and data double-sided, Is it that I used it wrong? expecting a reply !

manuel-mauky commented 5 years ago

In JavaFX there are two ways to bind properties:

  1. unidirectional bindings are created with the bind method. They support data-flow only in one direction.
  2. bidirectional bindings are created with the 'bindBidirectional' method.

Based on your description I assume that you've used a unidirectional binding from the model-value to the textfield like this:

textfield.textProperty().bind(viewModel.modelValueProperty());

Then the value can only go from the model to the view and not the other way around. Instead you should use a bi-directional binding like this:

textfield.textProperty().bindBidirectional(viewModel.modelValueProperty());

unidirectional bindings are useful if the target-property isn't changed by the user. For example the disableProperty of Controls can be bound this way so that the viewModel provides a (read-only) boolean-property for this.