1

我在使用数据绑定同步两个 Eclipse 部件时发现了问题,我在两个部件类之间使用了一个模型,其中包含文本框,并且两个类都与该模型绑定。

对于第一部分:

DataBindingContext databindingcontext = new DataBindingContext();
textLeft = new Text(parent, SWT.BORDER);
textLeft.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
IObservableValue iobservableModel = PojoProperties.value("name").observe(new Name());
IObservableValue observablevalueLeft = WidgetProperties.text (SWT.Modify).observe(textLeft);
databindingcontext.bindValue (observablevalueLeft,iobservableModel);    

模型:

public class Name {
    public static String static_strName;

    public String getName() {
        return static_strName;
      }

      public void setName(String name) {
        static_strName = name;
      }
}

第 2 部分:

DataBindingContext m_databindingcontext = new DataBindingContext();
textPart2 = new Text(parent, SWT.BORDER);
textPart2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
IObservableValue iobservableModel = PojoProperties.value("name").observe(new Name());
IObservableValue observablevaluePart2 = WidgetProperties.text (SWT.Modify).observe(textPart2);
m_databindingcontext.bindValue (observablevaluePart2,iobservableModel);       
4

1 回答 1

0

试试这个:你需要 PropertyChangeSupport

DataBindingContext bindingContext = new DataBindingContext();
textLeft = new Text(parent, SWT.BORDER);
textLeft.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
IObservableValue observablevalueLeft = SWTObservables.observeText(textLeft ,                    SWT.Modify);
IObservableValue iobservableModel = BeansObservables.observeValue(modelChangeRegistry,     "name");
bindingContext.bindValue(iobservableModel, iobservableModel , null, null);

public class Name extends AbstractPropertyRegistration {

    public static String static_strName;
        public String getName() {
        return static_strName;
    }
    public void setName(String name) {
       static_strName = name;
    }
}

public abstract class AbstractPropertyRegistration {
private final PropertyChangeSupport propertyChangeSupport = new       PropertyChangeSupport(this);
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}
public void addPropertyChangeListener(String propertyName, PropertyChangeListener   listener) {
propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}

public void removePropertyChangeListener(String propertyName, PropertyChangeListener   listener) {
propertyChangeSupport.removePropertyChangeListener(propertyName, listener);
}

protected void firePropertyChange(String propertyName, Object oldValue, Object   newValue) {
propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
}
}
于 2013-09-28T15:55:43.510 回答