我有自定义样式的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
。
答案 0 :(得分:0)
访问样式元素的标准方法是通过journey_role_user
并迭代子样式对象。
尝试一下:
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
适用于10.2