我正在开发 Eclipse RCP 应用程序,我需要创建一个更大的对话框,我需要在其中处理数据绑定(bind)。使用 TextFields 可以很好地进行绑定(bind)。我对 2 个 SWT 小部件有问题 - Combo、Spinner。 Spinner 中会有数字,我也可以在其中书写。 Spinner 仅使用字符串数组中的值。
对于绑定(bind),我使用以下代码:
private void doBinding(MyPartComposite comp) {
/* code for spinner */
IObservableValue model = PojoProperties.value(MyPart.class, "age").observe(comp.result); //$NON-NLS-1$
ISWTObservableValue target = WidgetProperties.text(SWT.Modify).observe(comp.ageSpinner);
UpdateValueStrategy targetToModel = new UpdateValueStrategy(UpdateValueStrategy.POLICY_CONVERT);
this.dataBindingContext.bindValue(target, model, targetToModel, null);
/* code for combo */
model = PojoProperties.value(MyPart.class, "gender").observe(comp.result); //$NON-NLS-1$
target = WidgetProperties.text(SWT.Modify).observe(comp.genderCombo);
targetToModel = new UpdateValueStrategy(UpdateValueStrategy.POLICY_CONVERT);
this.dataBindingContext.bindValue(target, model, targetToModel, null);
}
最佳答案
Spinner
没有文本属性,而是将其绑定(bind)到其选择。下面的示例对我有用,我假设年龄属性是整数,性别是字符串。
IObservableValue observeSelectionAgeSpinnerObserveWidget = WidgetProperties.selection().observe(ageSpinner);
IObservableValue ageResultObserveValue = PojoProperties.value("age").observe(comp.result);
dataBindingContext.bindValue(observeSelectionAgeSpinnerObserveWidget, ageResultObserveValue, null, null);
//
IObservableValue observeSingleSelectionGenderCombo = ViewerProperties.singleSelection().observe(genderCombo);
IObservableValue genderResultObserveValue = PojoProperties.value("gender").observe(comp.result);
dataBindingContext.bindValue(observeSingleSelectionGenderCombo, genderResultObserveValue, null, null);
关于java - SWT Widgets 绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55982025/