5

我有自定义样式的 FireMonkey 控件。它的样式包含多个级别的嵌套控件。

我需要在运行时访问这些控件并更改一些样式属性。为此,我正在使用FindStyleResource<T>方法。

我在检索样式内部的第一级控件方面没有问题。但是,FindStyleResource如果控件父级是TStyledControl.

问题是如何访问那些嵌套样式控件,而不管它们的父类型如何?

风格:

object TStyleContainer
  object TLayout
    StyleName = 'MyHeader'
    Align = Center
    Size.Width = 100.000000000000000000
    Size.Height = 50.000000000000000000
    Size.PlatformDefault = False
    Visible = False
    TabOrder = 0
    object TLabel
      StyleName = 'title'
      Align = Client
      StyledSettings = [Style]
      Size.Width = 36.000000000000000000
      Size.Height = 50.000000000000000000
      Size.PlatformDefault = False
      TextSettings.HorzAlign = Center
      Text = 'Title'
    end
    object TLayout
      StyleName = 'green'
      Align = MostLeft
      Size.Width = 32.000000000000000000
      Size.Height = 50.000000000000000000
      Size.PlatformDefault = False
      object TPath
        StyleName = 'greenpath'
        Align = Fit
        Data.Path = {
          07000000000000000000404100000041010000000000C0400000604101000000
          B81EED405C8F76410100000000004041AE472D410100000052B884415C8F7641
          010000000000904100006041030000000000404100000041}
        Fill.Color = claGreen
        HitTest = False
        Size.Width = 32.000000000000000000
        Size.Height = 50.000000000000000000
        Size.PlatformDefault = False
        Stroke.Kind = None
        WrapMode = Fit
      end
    end
    object TSpeedButton
      StyleName = 'red'
      Align = MostRight
      Position.X = 68.000000000000000000
      Size.Width = 32.000000000000000000
      Size.Height = 50.000000000000000000
      Size.PlatformDefault = False
      object TPath
        StyleName = 'redpath'
        Align = Fit
        Data.Path = {
          07000000000000000000404100000041010000000000C0400000604101000000
          B81EED405C8F76410100000000004041AE472D410100000052B884415C8F7641
          010000000000904100006041030000000000404100000041}
        Fill.Color = claRed
        HitTest = False
        Size.Width = 32.000000000000000000
        Size.Height = 32.571426391601560000
        Size.PlatformDefault = False
        Stroke.Kind = None
        WrapMode = Fit
      end
    end
  end
end

控制:

type
  TMyHeader = class(TStyledControl)
  protected
    procedure ApplyStyle; override;
    function GetDefaultStyleLookupName: string; override;
   end;

procedure TMyHeader.ApplyStyle;
var
  LGreen: TLayout;
  LGreenPath: TPath;
  LRed: TSpeedButton;
  LRedPath: TPath;
begin
  inherited;
  if FindStyleResource<TLayout>('green', LGreen) then
    begin
      // following call will find greenpath control 
      if FindStyleResource<TPath>('greenpath', LGreenPath) then
        LGreenPath.Fill.Color := TAlphaColorRec.Blue;
    end;

  if FindStyleResource<TSpeedButton>('red', LRed) then
    begin 
      // following call will fail to find find redpath control  
      if FindStyleResource<TPath>('redpath', LRedPath) then
        LRedPath.Fill.Color := TAlphaColorRec.Blue;

      // this variant also fails
      if LRed.FindStyleResource<TPath>('redpath', LRedPath) then
        LRedPath.Fill.Color := TAlphaColorRec.Blue;
    end;
end;

function TMyHeader.GetDefaultStyleLookupName: string;
begin
  Result := 'MyHeader';
end;

原创风格:

在此处输入图像描述

更改样式(仅成功更改绿色箭头颜色)

在此处输入图像描述

ApplyStyle方法中,我可以greenpath从样式中访问并将其颜色更改为蓝色。但是,我无法redpath使用FindStyleResource方法。

4

1 回答 1

0

访问样式元素的标准方法是通过TFMXObject并迭代子样式对象。

尝试这个:

procedure TMyHeader.ApplyStyle;
var
  objFMX,
  inObjFMX: TFMXObject;
  LRed: TSpeedButton;
  LRedPath: TPath;
begin
  inherited;
  objFMX:=FindStyleResource('red');
  if assigned(objFMX) and (objFMX is TSpeedButton) then
  begin
    LRed:=objFMX as TSpeedButton;
    inObjFMX:=LRed.FindStyleResource('redpath');
    if assigned(inObjFMX) and (inObjFMX is TPath) then
    begin
      LRedPath:=inObjFMX as TPath;
     LRedPath.Fill.Color := TAlphaColorRec.Blue;
   end;
  end
end;

更新的代码:在上面的FindStyleResource代码中不起作用。下面采用不同的方法。

procedure TMyHeader.ApplyStyle;
var
  objFMX,
  inObjFMX: TFMXObject;
  LRedPath: TPath;
begin
  inherited;
  objFMX:=FindStyleResource('red');
  if assigned(objFMX) and (objFMX is TSpeedButton) then
  begin
    for inObjFMX in objFMX.Children do
    begin
      if inObjFMX is TPath then
      begin
        LRedPath:=inObjFMX as TPath;
        LRedPath.Fill.Color:=TAlphaColorRec.Blue;
        Break;
      end;
    end;
  end;
end;

这适用于 10.2

于 2018-09-25T07:53:47.410 回答