Skip to content

双向绑定

双向绑定 用 <=> 书写,链接两个属性,使它们始终持有相同的值; 写入任一侧都会更新另一侧:

export component Example {
in property<brush> rect-color <=> r.background;
// The type may be omitted; it is then inferred from the other side.
in property rect-color2 <=> r.background;
r := Rectangle {
background: blue;
}
}
slint

左侧是被声明或绑定的属性或回调; 右侧引用它所链接到的属性或回调。

  • 两侧必须具有相同的类型。 在属性声明中可以省略类型,并从另一侧推断。
  • 右侧引用某个属性——同一元素的、另一个元素的、根组件的, 或全局对象的(Global.property)——或结构体类型属性内类型匹配的字段。 链接到全局对象的属性会在组件上重新暴露该成员。
  • 在 for 元素内,右侧可以是模型值, 它会保持同步。
  • 两侧在输入和输出上必须兼容:在绑定读取或写入它们的地方,每一侧都必须可读且可写。
  • 链接对的初始值是右侧的值。

相同的 <=> 语法构成回调别名,将一个回调转发到另一个回调(见回调)。 回调别名可以指向全局对象的回调,包括来自另一个全局对象——这是受支持的方式, 用于让一个全局对象实现另一个的回调。

global Logic {
in-out property <int> the-value;
pure callback magic-operation(int) -> int;
}
export component MainWindow inherits Window {
// re-expose the global's members on the component
in-out property the-value <=> Logic.the-value;
pure callback magic-operation <=> Logic.magic-operation;
}
slint

© 2026 SixtyFPS GmbH