C++ Builder运行时单向或双向动态绑定控件的例子

来源:互联网 发布:网络老虎机揭秘 编辑:程序博客网 时间:2024/05/01 10:32

// 在构造函数里创建绑定对象

__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{

  // 创建绑定集合列表

  BindingsList1 = new TBindingsList(this);


  // 建立单向绑定并激活

  TBindExpression * BindExpress1 = new TBindExpression(BindingsList1);
  BindExpress1->ControlComponent = Label1;   // 目标控件
  BindExpress1->ControlExpression = "Text";  // 目标控件属性
  BindExpress1->SourceComponent = TrackBar1; // 源控件

  BindExpress1->SourceExpression ="Format(\"进度: %.0f%%\", Value)";   // 源控件属性

  BindExpress1->Active = true;               // 激活, Active = false 是断开绑定


  // 建立双向绑定并激活
  TBindExpression * BindExpress2 = new TBindExpression(BindingsList1);
  BindExpress2->ControlComponent = Edit1;    // 目标控件
  BindExpress2->ControlExpression = "Text";  // 目标控件属性
  BindExpress2->SourceComponent = TrackBar2; // 源控件

  BindExpress2->SourceExpression = "Value";  // 源控件属性

  BindExpress2->Direction = TExpressionDirection::dirBidirectional; // 双向绑定

  BindExpress2->Active = true;               // 激活, Active = false 是断开绑定

}


// 动态更新第1个表达式的数据

void __fastcall TForm1::TrackBar1Change(TObject *Sender)
{
  BindingsList1->Notify(Sender, "Value");
}

// 动态更新第1个表达式的数据
void __fastcall TForm1::TrackBar2Change(TObject *Sender)
{
  BindingsList1->Notify(Sender, "Value");
}

void __fastcall TForm1::Edit1Change(TObject *Sender)
{
  BindingsList1->Notify(Sender, "");
}

0 0
原创粉丝点击