如何强调cxDBchecklistbox中的一些特定项目

How to emphasize some specific items from cxDBchecklistbox

我有两个不同来源的数据集 - 一些用于政府,另一些仅用于组织。我需要以某种方式区分它们,比如将一些项目设为粗体或不同的颜色。

我尝试使用 DrawItem 事件,但无法弄清楚。

为了添加我使用的项目:

  while not (cdDataset1.Eof) do
   begin
     if ((cdDataset1.fieldbyName('displayName').value<> '')  and (cdDataset1.fieldbyName('TyypId').value=1280781)) then
     begin
     cxDBCheckListBox1.Items.Add.Text:= cdDataset1.fieldbyName('displayName').value;
     end;
     cdDataset1.Next;
   end;

   cdDataset1.First;

   while not (cdDataset1.Eof) do
   begin
   if ((cdDataset1.fieldbyName('displayName').value<> '')  and (cdDataset1.fieldbyName('TyypId').value=1243501)) then
     begin
     cxDBCheckListBox1.Items.Add.Text:= cdDataset1.fieldbyName('displayName').value;
     end;
     cdDataset1.Next;
   end;

这部分效果很好。但是我可以在 cxdbchecklistbox 上使用字段 TyypID 来区分吗? 它应该看起来像这样(当然是复选框而不是项目符号):

我找到了解决方案。首先添加 tags 。需要创建 canvas 的 OnDrawItem:

//adding items to checklistbox where needed. 
    cxDBCheckListBox1.Items.Clear;
    while not (cdDataset1.Eof) do
    begin
       if ((cdDataset1.fieldbyName('displayName').value<> '') and 
           (cdDataset1.fieldbyName('TyypId').value=28078)) then
            begin
              with cxDBCheckListBox1.Items.Add do
                begin
                   Text:= cdDataset1.fieldbyName('displayName').value;
                   Tag := 1280781;
                end;
             end;
             cdDataset1.Next;
    end;

//Then on drawitem:

procedure TfmSample.cxDBCheckListBox1DrawItem(
  Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
 var
  ACanvas: TcxCanvas;
  AText: string;
  ATextRect: TRect;
  AGlyphWidth: Integer;
  AListBox: TcxDBCheckListBox;
  ACanvasFont: TFont;
  AItemEnabled: Boolean;
  AItemTag: Integer;
begin
  AListBox := (Control as TcxDBCheckListBox);
  ACanvas := AListBox.InnerCheckListBox.Canvas;
  ACanvasFont := ACanvas.Font;
  AItemTag := AListBox.Items[Index].Tag;
  AItemEnabled := AListBox.Items[Index].Enabled;

  case AItemTag of
     1243501:
      begin
        ACanvasFont.Color := clBlue;
      end;
    1280781 :
      begin
        ACanvasFont.Style := [fsBold];
        ACanvasFont.Color := clBlack;
      end;
  end;
  ACanvas.Brush.Color := clWhite;
   ACanvas.FillRect(Rect);
  AText := AListBox.Items[Index].Text;
  ATextRect := Rect;
  ATextRect.Left := 20;
  ACanvas.DrawTexT(AText, ATextRect, 0);
end;