jeudi 14 septembre 2017

How to make bidirectional binding to selected item in JavaFX ComboBox using MVVM?

I implement MVVM pattern. So, in ViewModel I have:

public class ViewModel {
   private ObservableList<User> users = FXCollections.observableArrayList();
   private ObjectProperty<User> selectedUser = new SimpleObjectProperty<>();
   public ObservableList<User> getUsers() {...}
   public ObjectProperty<User> selectedUserProperty() {...}
}

And in view I do

public class View {
  ....
  private ComboBox<User> userComboBox;
  public void bindItAll() {
      Bindings.bindContentBidirectional(userComboBox.getItems(), viewModel.getUsers()); 
  }
}

The problem is that I need to make bidirectional binding to selected user. I tried to do it via userComboBox.getSelectionModel().selectedItemProperty() but it returns ReadOnly property. What is the right way to do it?

Aucun commentaire:

Enregistrer un commentaire