(原创) Delphi中LiveBinding 绑定非数据库类数据的时候显示字段自定义名称(补充)
测试的时候又碰到一个小问题,不绑定不显示的域(但是这些域又必须存在于Entity类中,比如ID域等)。
简单修改就可以实现,Entity增加一个Attribute,比如FieldNoLink,标记此属性不绑定;AddFields方法也修改下,把默认的FieldList 中的不绑定域去除。
代码:
//Not Link JkFieldNoLink = class(TJkBindAttribute);
procedure TJkObjectBindSourceAdapter<T>.AddFields; var LType: TRttiType; //LFields: TArray<TRttiField>; //LField: TRttiField; LProps: TArray<TRttiProperty>; LProp: TRttiProperty; LAttrs: TArray<TCustomAttribute>; Lattr: TCustomAttribute; LRemoveField: TBindSourceAdapterField; begin inherited; LRemoveField := nil; CreateList; LType := GetObjectType; // 规定要显示的数据域全部是Published的属性 LProps := LType.GetProperties; for LProp in LProps do begin if LProp.Visibility = TMemberVisibility.mvPublished then begin LAttrs := LProp.GetAttributes; for Lattr in LAttrs do begin //No Link if Lattr is JkFieldNoLink then begin LRemoveField := Self.FindField(LProp.Name); if Assigned(LRemoveField) then begin Self.Fields.Remove(LRemoveField); end; end else //DisplayName begin if (Lattr is JkFieldLabel) and (not JkFieldLabel(Lattr).Name.IsEmpty) then begin FFieldLabelList.AddOrSetValue(LProp.Name, JkFieldLabel(Lattr).Name); end else if FFieldLabelList.ContainsKey(LProp.Name) then begin FFieldLabelList.Remove(LProp.Name); end; end; end; end; end; end;
TJkListBindSourceAdapter<T>.AddFields;也做同样的修改,OK