我在TImage的样式中添加了一个TListBoxItem。
如果我添加到一个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;发布于 2014-03-17 20:50:06
您真的不应该在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中使用ApplyStyle等(或者创建一个共享的方法来完成)。
在FormCreate中,创建新类的项,并酌情设置Bitmap属性。
至于高度问题,请尝试设置组合框的ItemHeight属性。如果你想在列表中有不同的高度,那么你可能就不走运了。
https://stackoverflow.com/questions/22441575
复制相似问题