Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 是否将DevXPress TcxFilterOperatorKind转换为字符串或从字符串转换为字符串?_Delphi_Enums_Devexpress_Rtti - Fatal编程技术网

Delphi 是否将DevXPress TcxFilterOperatorKind转换为字符串或从字符串转换为字符串?

Delphi 是否将DevXPress TcxFilterOperatorKind转换为字符串或从字符串转换为字符串?,delphi,enums,devexpress,rtti,Delphi,Enums,Devexpress,Rtti,下面是我用来从DevExpress网格中的筛选器获取filtertype运算符的代码片段: OperatorKindToStr用于将operatorkind作为字符串从筛选器中提取并存储在xml文件中。 StrToOperatorKind用于从xml转换回字符串,以在筛选器中设置operatorkind const CUSTFILTER_FILTERITEM = 'FilterItem'; function OperatorKindToStr(const aOperatorKind

下面是我用来从DevExpress网格中的筛选器获取filtertype运算符的代码片段: OperatorKindToStr用于将operatorkind作为字符串从筛选器中提取并存储在xml文件中。 StrToOperatorKind用于从xml转换回字符串,以在筛选器中设置operatorkind

const
  CUSTFILTER_FILTERITEM     = 'FilterItem';

function OperatorKindToStr(const aOperatorKind: TcxFilterOperatorKind): string;
begin
  Result := 'foEqual';
  case aOperatorKind of
    foEqual:        Result := 'foEqual';
    foNotEqual:     Result := 'foNotEqual';
    foLess:         Result := 'foLess';
    foLessEqual:    Result := 'foLessEqual';

  // Plus a boring list of other constants
end;

function StrToOperatorKind(const aOpKindStr: string): TcxFilterOperatorKind;
begin
  Result := foEqual;
  if aOpKindStr       = 'foNotEqual' then
    Result := foNotEqual
  else if aOpKindStr  = 'foLess' then
    Result := foLess
  else if aOpKindStr  = 'foLessEqual' then
    Result := foLessEqual
  else if aOpKindStr  = 'foGreater' then
    Result := foGreater
  else if aOpKindStr  = 'foGreaterEqual' then
    Result := foGreaterEqual

  // Plus a boring list of other if-else
end;

procedure UseStrToOperatorKind(const aFilterItem: IXmlDomElement);
begin
  if aFilterItem.nodeName = CUSTFILTER_FILTERITEM then
  begin                              // It is an FilterItem
    vStr := VarToStr(aFilterItem.getAttribute(CUSTFILTER_COLPROP));  // Get the columnname
    vOperatorKind := StrToOperatorKind(aFilterItem.getAttribute(CUSTFILTER_ITEMOPERATOR));
end;

procedure UseOperatorKindToStr(const aFilterItem: TcxCustomFilterCriteriaItem);
var
  vStr: String;
begin
  if Supports(TcxFilterCriteriaItem(aFilterItem).ItemLink, TcxGridColumn, GridCol) then
    vStr := OperatorKindToStr(TcxFilterCriteriaItem(aFilterItem).OperatorKind);
end;
显然我希望strotoperatorkind和OperatorKindToStr更聪明一点。 我在VCL TypeInfo中尝试了GetEnumProp方法,但它不起作用。
那么,如何将TcxFilterOperatorKind属性从aFilterItem变量提取为字符串并返回到TcxFilterOperatorKind?

GetEnumProp无法工作,因为它是错误的函数。不过你很接近。尝试GetEnumName和GetEnumValue,它们也在TypInfo单元中。

使用梅森指出的GetEnumNameGetEnumValue二重唱

您的功能应该变得更简单:

function OperatorKindToStr(const aOperatorKind: TcxFilterOperatorKind): string;
begin
  Result := GetEnumName(TypeInfo(TcxFilterOperatorKind), Ord(aOperatorKind));
end;

function StrToOperatorKind(const aOpKindStr: string): TcxFilterOperatorKind;
begin
  Result := TcxFilterOperatorKind(GetEnumValue(TypeInfo(TcxFilterOperatorKind), aOpKindStr));
end;