【问题标题】:Can we use boolean operator in Delphi XE2 LiveBinding expression?我们可以在 Delphi XE2 LiveBinding 表达式中使用布尔运算符吗?
【发布时间】:2012-08-03 09:04:44
【问题描述】:

我打算为 TCheckBox 写一个 Live Binding 源表达式:

SourceExpression = '(Checked = False) and (Enabled = True)'

执行代码时,提示异常:

Expected EOF - trailing text in expression

Delphi XE2 Live Binding 是否支持布尔运算符?

【问题讨论】:

  • 只是一个疯狂的猜测 - 您是否尝试过使用 SourceExpression = '(not Checked) and (Enabled)' ?为什么?因为每当我在布尔表达式中看到 = 时都会伤到我的眼睛 :-) 但是现在没有 Delphi XE2...
  • not Checked 不支持,所以我使用Checked = False
  • 看看这个 SO 问题:How to write live binding expression that control TEdit.PasswordChar based on TCheckBox.Checked?。接受的答案注册一个布尔表达式评估器。
  • AFAIK 你不能在 LiveBinding 表达式中使用逻辑运算符(andor、'not'、'xor'、'shl'、'shr'):(,如解决方法,您可以注册一个自定义方法来评估逻辑操作。
  • shl 和 shr 不是逻辑运算符。无法相信实时绑定不支持逻辑运算符。

标签: delphi delphi-xe2 livebindings


【解决方案1】:

不,不直接支持布尔运算符。

来自docu

您可以在表达式中使用以下数学符号:

  • 常量:nil True False Pi
  • 算术运算符:+ - * /
  • 逻辑运算符:= >=
  • 括号 (),用于更改运算符优先级。

但您可以使用the builtin functions IfAll(), IfAny() and IfThen() 代替andornot

SourceExpression := 'IfAll(IfThen(Checked, False, True), Enabled)'

或者你register your own functions

我测试了这 4 个 XE4,但它应该适用于 XE2 2。

  • A or B: IfAny(A, B)
  • A and B: IfAll(A, B)
  • not A: IfThen(A, False, True)
  • A xor B: IfAll(IfAny(A, B), IfThen(IfAll(A,B), False, True))

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多