zoukankan      html  css  js  c++  java
  • Delphi XE2 之 FireMonkey 入门(30) 数据绑定: TBindingsList: TBindExpression 的 OnAssigningValue 事件


    表达式中的函数有限, 譬如我想通过绑定输出文本的长度(譬如在 Label1 中绑定输出 Edit1.Text 的长度)就没有相应的函数;

    这可在 TBindExpression 的 OnAssigningValue 事件中处理.

    TBindExpression 和它的兄弟们 (TBindExprItems、TBindLink、TBindListLink、TBindGridLink、TBindPosition、TBindList、TBindGridList、TBindDBEditLink、TBindDBTextLink、TBindDBListLink、TBindDBImageLink、TBindDBMemoLink、TBindDBCheckLink、TBindDBGridLink) 拥有相同的事件.

    先在窗体上添加 Label1、Edit1、BindingsList1, 然后激活 Edit1 的 OnKeyUp 事件和窗体的默认事件;
    代码中手动完成了 OnAssingningValue 事件, 在设计时添加事件会更方便.

    unit Unit1;
    
    interface
    
    uses
      System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types,
      FMX.Controls, FMX.Forms, FMX.Dialogs, Data.Bind.EngExt, Fmx.Bind.DBEngExt, System.Rtti,
      System.Bindings.Outputs, FMX.Layouts, Data.Bind.Components, FMX.Edit;
    
    type
      TForm1 = class(TForm)
        Label1: TLabel;
        Edit1: TEdit;
        BindingsList1: TBindingsList;
        procedure FormCreate(Sender: TObject);
        procedure Edit1KeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
        procedure MyOnAssigningValue(Sender: TObject; AssignValueRec: TBindingAssignValueRec; var Value: TValue; var Handled: Boolean);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.fmx}
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      with TBindExpression.Create(BindingsList1) do
      begin
        ControlComponent := Label1;
        ControlExpression := 'Text';
        SourceComponent := Edit1;
        SourceExpression := 'Text';
        OnAssigningValue := MyOnAssigningValue; //
        Active := True;
      end;
    end;
    
    procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
    begin
      BindingsList1.Notify(Sender, '');
    end;
    
    procedure TForm1.MyOnAssigningValue(Sender: TObject; AssignValueRec: TBindingAssignValueRec; var Value: TValue; var Handled: Boolean);
    begin
      Value := Length(Value.ToString); //
    end;
    
    end.
    

  • 相关阅读:
    F2. Same Sum Blocks (Hard) 解析(思維、前綴和、貪心)
    E. Copying Data 解析(線段樹)
    B. Nauuo and Circle 解析(思維、DP)
    POJ3436-ACM Computer Factory(最大流)
    A.牛牛扔牌(双端队列)/B.疯狂过山车(最长上升子序列)/C.牛牛的棋盘(容斥原理)
    CodeForces 665E. Beautiful Subarrays(字典树)(贪心)(异或前缀和)
    CodeForces 455C.Civilization(并查集)(树的直径)
    CodeForces 1021B. Chemical table(并查集)
    CodeForces 961E. Tufurama(主席树)
    CodeForces 1024C. Array Product(模拟)(分类讨论)
  • 原文地址:https://www.cnblogs.com/del/p/2198508.html
Copyright © 2011-2022 走看看