【问题标题】:TComboBox with Image in FiremonkeyFiremonkey中带有图像的TComboBox
【发布时间】:2014-03-16 19:15:23
【问题描述】:

我在 TListBoxItem 的样式中添加了一个 TImage。

如果我添加到 TListBox,它会起作用。如果我添加到 TComboBox,它就不起作用。如果 TComboBox 中的项目,我什至无法更改高度。

这是我的示例代码:

procedure TMainForm.FormCreate(Sender: TObject);
const
  BitmapFile : String = 'F:\testimage.png';
var
  ItemText : TText;
  ItemImage   : TImage;
  ListBoxItem : TListBoxItem;
  button : TButton;
begin

  ListBoxItem := TListBoxItem.Create(nil);
  ListBoxItem.Parent := CBoxHeadMenuLanguage;

  ListBoxItem.StyleLookup := 'ListBoxItemIconStyle';
  ListBoxItem.Height := 50; //just for test

  ItemText := ListBoxItem.FindStyleResource('text') as TText;
  if Assigned(ItemText) then ItemText.Text := 'Hello World!';

  ItemImage := ListBoxItem.FindStyleResource('image') as TImage;
  if Assigned(ItemImage) then If FileExists(BitmapFile) Then ItemImage.Bitmap.LoadFromFile(BitmapFile);
end;

【问题讨论】:

    标签: delphi firemonkey delphi-xe5 tlistbox tcombobox


    【解决方案1】:

    您真的不应该在 FormCreate 中进行样式设置,因为样式是根据需要应用的,并且可以随时删除和重新应用。

    相反,您需要使用 OnApplyStyleLookup 事件或 ApplyStyle 方法。我建议继承 TListBox 并使用后者并添加一个属性来存储位图。

    大纲类声明是:

    type TBitmapLBItem = class(TListBoxItem)
      private
        FBitmap: TBitmap;
      protected
        procedure ApplyStyle;override;
      public
        property Bitmap: TBitmap read FBitmap write SetBitmap;
      end;
    

    在 ApplyStyle 和 SetBitmap 中使用 FindStyleResource 等(或创建共享方法来执行此操作)。

    并在 FormCreate 中创建新类的项目并根据需要设置位图属性。

    至于高度问题,尝试设置组合框的ItemHeight属性。如果您想要列表中的各种高度,您可能不走运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      • 2014-09-09
      • 2013-02-02
      • 2023-01-27
      • 1970-01-01
      • 2014-08-22
      • 2013-08-27
      相关资源
      最近更新 更多