【问题标题】:How to bind enum values with SWT Combo如何使用 SWT Combo 绑定枚举值
【发布时间】:2018-01-23 12:58:43
【问题描述】:

我想将枚举值绑定到 SWT Combo。

让我们说我有以下代码 sn-p

人物模型类

public class Person {
 private String name;
 private Gender gender;

 public setGender(Gender gender) {
   this.gender = gender;
 }

 public Gender getGender() {
   return gender;
 }
}

性别枚举

public enum Gender {
  MALE("male"), FEMALE("female");
}

组合选择与 Person 的性别属性之间的绑定。

IObservableValue<?> observeWidget = WidgetProperties.selection().observe(combo);                
IObservableValue<?> observeModel = PojoProperties.value(person.getClass(), "gender").observe(person);

组合值和枚举 Gender 之间的绑定。

IObservableList<?> observeWidget = WidgetProperties.items().observe(combo);
IObservableList<?> observableList = PojoProperties.list(enumGender.getClass(), "???? property name ????").observe(enumGender);

我怎样才能达到以上绑定??

我想用 SWT 组合绑定 Gender.values()(这个方法默认在每个枚举中并返回一个字符串数组)。 对于绑定,我们必须在 API 调用中提供属性名称,但 enum 不包含任何此类属性。

我知道我们可以使用 ComboBoxViewer 来实现这一点 comboViwer.setInput(Gender.values())。 但我想用组合绑定任何枚举。

【问题讨论】:

    标签: eclipse-plugin swt eclipse-rcp jface


    【解决方案1】:

    你检查了 sn-p Snippet034:

    https://github.com/eclipse/eclipse.platform.ui/blob/master/examples/org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet034ComboViewerAndEnum.java

    根据您使用的版本,您可能必须将调用调整为传递更少/更多参数的“bindValue”

    【讨论】:

    • 我检查了 sn-p 他们没有使用枚举值和组合框的绑定。他们刚刚完成了gender.setInput(Gender.values());
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 2018-06-09
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 2016-03-26
    相关资源
    最近更新 更多