You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在自定义FMX控件中实现IGlyph接口并关联图像属性?

Delphi FMX自定义SpeedButton集合控件设计时属性编辑器问题

我正在开发一个显示TSpeedButton控件集合的自定义FMX控件,通过TCollection和TCollectionItem实现了设计时用标准属性编辑器管理按钮的功能。目前已经通过控件的Images属性(基于TCustomImageList)实现IGlyph接口,为按钮提供Glyph资源,但遇到两个问题:

  • 无法将顶层控件的Images属性与每个ButtonItem的ImageIndex关联,导致设计时属性编辑器无法显示可用图像的弹出选择列表;
  • 用于分配FMX样式中Glyph的StyleLookup属性编辑器未在设计时显示。

我已经研究了好几天,参考过TListBox等控件的源码还是没找到头绪,希望能得到解决方向的指引。


解决方向指引

1. 关联Images属性与ImageIndex的弹出列表

  • 为ButtonItem的ImageIndex属性自定义属性编辑器,继承自TImageIndexProperty,在编辑器中获取顶层控件的Images属性作为图像来源。
  • 在编辑器的GetImageList方法中,通过当前编辑的ButtonItem获取其所属的自定义集合控件,进而拿到Images属性实例。
  • 注册该自定义属性编辑器,关联到ButtonItem的ImageIndex属性,核心代码示例:
    type
      TButtonItemImageIndexProperty = class(TImageIndexProperty)
      public
        function GetImageList: TCustomImageList; override;
      end;
    
    function TButtonItemImageIndexProperty.GetImageList: TCustomImageList;
    var
      LItem: TButtonItem;
      LControl: TCustomSpeedButtonCollectionControl;
    begin
      LItem := GetComponent(0) as TButtonItem;
      if LItem.Owner is TCustomSpeedButtonCollectionControl then
      begin
        LControl := TCustomSpeedButtonCollectionControl(LItem.Owner);
        Result := LControl.Images;
      end
      else
        Result := inherited GetImageList;
    end;
    
    // 注册属性编辑器
    procedure Register;
    begin
      RegisterPropertyEditor(TypeInfo(Integer), TButtonItem, 'ImageIndex', TButtonItemImageIndexProperty);
    end;
    

2. 显示StyleLookup属性编辑器

  • 确保ButtonItem类的StyleLookup属性为published的string类型。
  • 为该属性注册FMX内置的TStyleLookupProperty编辑器,这是专门用于样式查找的属性编辑器,注册代码示例:
    procedure Register;
    begin
      RegisterPropertyEditor(TypeInfo(string), TButtonItem, 'StyleLookup', TStyleLookupProperty);
    end;
    
  • 同时需确保自定义控件和ButtonItem正确集成FMX样式系统,比如ButtonItem对应的样式目标类型已正确定义,或控件初始化时关联了样式上下文。

内容的提问来源于stack exchange,提问作者Bob Baudewyns

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 09:22:08