如何在 TcxExtLookupComboBox 中使用 TcxCustomDataSource?

2024-01-16

我使用 Devexpress 的 TcxExtLookupComboBox 并尝试实现自定义数据源。我已经像这样设置了自定义数据源:

procedure TMainForm.FormCreate(Sender: TObject);
begin
  fDataSource := TMyDataSource.Create;
  cbotestSearch.Properties.View.DataController.CustomDataSource := fDataSource;
end;

TMyDataSource 定义如下:

unit Datasource;

interface

uses
  Classes,
  IBQuery,
  SysUtils,
  cxCustomData;

type
  TSearchItem = class
    private
      BoldID: String;
      Display: String
  end;

  TMyDataSource = class(TcxCustomDataSource)
  private
    fSearchList: TList;
  protected
    function GetRecordCount: Integer; override;
    function GetValue(ARecordHandle: TcxDataRecordHandle; AItemHandle: TcxDataItemHandle): Variant; override;
  public
    constructor Create;
    destructor Destroy; override;
    procedure GetData;
  end;

implementation

constructor TMyDataSource.Create;
begin
  inherited Create;
  fSearchList := TList.Create;
end;

destructor TMyDataSource.Destroy;
begin
  FreeAndNil(fSearchList);
  inherited;
end;

procedure TMyDataSource.GetData;
var
  vItem: TSearchItem;
begin
  fSearchList.Clear;

  vItem := TSearchItem.Create;
  vItem.BoldID  := '1000';
  vItem.Display := 'test';
  fSearchList.Add(vItem);

  vItem := TSearchItem.Create;
  vItem.BoldID  := '1100';
  vItem.Display := 'test2';
  fSearchList.Add(vItem);

  DataChanged;    // Don't do anything as provider is nil
end;

function TMyDataSource.GetRecordCount: Integer;
begin
  // Is never entered
  Result := fSearchList.Count;
end;

function TMyDataSource.GetValue(ARecordHandle: TcxDataRecordHandle;
  AItemHandle: TcxDataItemHandle): Variant;
begin
  // Is never entered
  Result := 'Test';
end;

end.

问题是 TMyDataSource.GetValue 从未被调用。有任何提示如何修复吗?

更新1:我这里还有另一个提示。如果我单步执行 DataChanged 方法,应该会导致调用 GetValue,如下所示:

procedure TcxCustomDataSource.DataChanged;
begin
  if Provider = nil then Exit;

  // Code using Provider
end;

在这种情况下,Provider 为零。但我已经在 Forms oncreate 中分配了数据源,如您所见。


cxExtLookupComboBox 只能与 DB~views 一起使用。此类视图无法接受 TcxCustomDataSource 对象的实例作为数据源。因此,您的代码将无法工作:-(。有建议在将来实现此功能,并在以下位置注册:

http://www.devexpress.com/Support/Center/ViewIssue.aspx?issueid=AS10025 http://www.devexpress.com/Support/Center/ViewIssue.aspx?issueid=AS10025

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 TcxExtLookupComboBox 中使用 TcxCustomDataSource? 的相关文章

随机推荐