【问题标题】:How to access nested style control如何访问嵌套样式控件
【发布时间】:2019-02-27 22:16:13
【问题描述】:

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

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

我在检索样式内部的第一级控件时没有问题。但是,如果父控件是TStyledControl 的后代,则使用FindStyleResource 访问二级控件会失败。

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

风格:

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 并将其颜色更改为蓝色。但是,我无法使用FindStyleResource 方法获得redpath。

【问题讨论】:

  • 仅调用部分查找redpath控件时是否适用更改?
  • 我建议你使用LRedPath:= FindStyleResource('redpath') as TPath; If Assigned(LRedPath) then ...
  • @Sami 不,在任何组合中都找不到 redpath。失败和成功之间的唯一区别是 parent 是否是 TStyledControl 的后代。
  • @Sami 您的建议不会改变任何事情,因为 FindStyleResource 的通用版本调用了非通用变体。

标签: delphi firemonkey delphi-10.2-tokyo firemonkey-style


【解决方案1】:

访问样式元素的标准方法是通过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

【讨论】:

  • 我将不得不检查,但这是我的代码的细微变化,它不起作用。 FindStyleResource 找不到 redpath
  • 是的,这段代码也有同样的问题。 FindStyleResource 已损坏。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 2019-06-09
  • 1970-01-01
相关资源
最近更新 更多