我定義隨後的單元業務邏輯的Delphi Livebinding對象和組合框或單選按鈕
unit Models.Person;
interface
Type
TPersonGender = (pgUndefined, pgMale, pgFemale, pgNotApplicable);
TSexOfPerson = class(TPersistent)
private
FGender : TPersonGender;
protected
function GetDescription : string;
function GetCode : string;
function GetIndex : integer;
public
constructor Create; overload;
constructor Create(const aValue : TGenderPerson); overload;
procedure Assign(Source: TPersistent); override;
property Gender : TGenderPerson read FGender write FGender;
property Description : string read GetDescription;
property Code : string read GetCode;
property Index : integer read GetIndex;
end;
TPerson = class(TPersistent)
private
FSex : TSexOfPerson;
FName : string;
FSurName : string;
FAddress : string;
protected
function GetSex : TPersonGender;
procedure SetSex(aGender : TPersonGender);
public
constructor Create; overload;
constructor Create(const aValue : TPerson); overload;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
property Name : string read FName write FName;
property SurName : string read FSurName write FSurName;
property Address : string read FAddress write FAddress;
property Sex : TPersonGender read GetSex write SetSex
end;
implementation
{ TSexOfPerson }
constructor TSexOfPerson.Create;
begin
inherited Create;
FGender := pgUndefined;
end;
constructor TSexOfPerson.Create(const aValue : TPersonGender);
begin
inherited Create;
FGender := aValue
end;
procedure TSexOfPerson.Assign(Source: TPersistent);
begin
if Source is TSexOfPerson then
FGender := TSexOfPerson(Source).Gender
else
inherited Assign(Source)
end;
function TSexOfPerson.GetDescription;
begin
case FGender of
pgUndefined : Result := '<Undefined>';
pgMale : Result := 'Male';
pgFemale : Result := 'Female';
pgNotApplicable : Result := '<Not applicable>';
end
end;
function TSexOfPerson.GetIndex;
begin
Result := Ord(FGender)
end;
function TSexOfPerson.GetCodice;
begin
case FGender of
pgUndefined : Result := '';
pgMale : Result := 'M';
pgFemale : Result := 'F';
pgNotApplicable : Result := 'N'
end
end;
{ TPerson }
constructor TPerson.Create;
begin
inherited Create;
FSex := TSexOfPerson.Create(pgUndefined)
end;
constructor TPerson.Create(const aValue : TPerson);
begin
inherited Create;
FSex := TSexOfPerson.Create(aValue)
end;
destructor TPerson.Destroy;
begin
FSex.Free;
inherited Destroy
end;
procedure TPerson.Assign(Source: TPersistent);
begin
if Source is TPerson then
begin
FName := TPerson(Source).Name;
FSurName := TPerson(Source).SurName;
FAddress := TPerson(Source).Address;
FSex.Gender := TPerson(Source).Sex;
end
else
inherited Assign(Source)
end;
function GetSex : TPersonGender;
begin
Result := FSex.Gender
end;
procedure SetSex(aGender : TPersonGender);
begin
if FSex.Gender <> aGender then
FSex.Gender := aGender
end;
end.
現在,我將設計用於編輯一個TPerson一種形式,有三個TEDIT和TCombobox選擇性別。
如何使用TCombobox的雙向livebinding?