简体   繁体   English

在TornadoFX中,如何使用objectBinding将BigDecimal属性绑定到另一个BigDecimal属性?

[英]In TornadoFX, how can I bind BigDecimal properites to another BigDecimal property using objectBinding?

I'm trying to create a binded BigDecimal property that is a calculation of two other BigDecimal properites, like this: 我试图建立一个绑定BigDecimal属性,它是其他两者的计算BigDecimal性质在,就像这样:

val caculatedProperty: ObjectProperty<BigDecimal> = objectBinding<Any, BigDecimal>(sumProperty, discountProperty) { ... }

...like I would do with StringProperty or IntegerProperty , and it would otherwise work without issues. ...就像我会使用StringPropertyIntegerProperty ,否则它将正常工作。 However, this time I get a type mismatch: it expects ObjectProperty<BigDecimal> , but receives ObjectBinding<BigDecimal?> . 但是,这一次我得到了一个类型不匹配:它期望使用ObjectProperty<BigDecimal> ,但是接收到ObjectBinding<BigDecimal?>

What's the problem here? 这是什么问题 Did I use the objectProperty() method incorrectly? 我没有正确使用objectProperty()方法吗? Should I use some other method to create a binding of a custom object? 是否应该使用其他方法来创建自定义对象的绑定?

EDIT: 编辑:

The issue was that I was trying to override the ObjectProperty<BigDecimal> from parent class with ObjectBinding<BigDecimal> . 问题是我试图使用ObjectBinding<BigDecimal>从父类覆盖ObjectProperty<BigDecimal> ObjectBinding<BigDecimal> The solution, in my case, was to make the original ObjectProperty<BigDecimal> abstract, and leave implementation to the classes that inherit it. 在我的情况下,解决方案是使原始ObjectProperty<BigDecimal>抽象,并将实现留给继承它的类。

All the xBinding functions returns bindings, not properties. 所有xBinding函数都返回绑定,而不返回属性。 If you think about it, how would a calculated property behave if it was writable? 如果您考虑一下,如果可写属性是可写的,它将如何表现? Should it write back into the underlying properties perhaps? 是否应该写回基础属性? TornadoFX actually supports these kinds of proxy properties as well, but I suspect that's not what you're after. TornadoFX实际上也支持这些代理属性,但是我怀疑那不是您想要的。

Are you wondering why you get ObjectBinding<BigDecimal?> instead of ObjectBinding<BigDecimal> ? 您是否想知道为什么要使用ObjectBinding<BigDecimal?>而不是ObjectBinding<BigDecimal>

The reason is that the underlying property might be null, and if so we might want to return null from the calculated binding as well. 原因是基础属性可能为null,如果这样,我们可能还希望从计算的绑定中返回null。

This normally doesn't create an issue, the property is still observable and would carry the correct calculated BigDecimal value. 这通常不会产生问题,该属性仍然是可观察的,并且将携带正确的计算得出的BigDecimal值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM