ffert2907
ffert2907

Reputation: 370

delphi How to change at runtime a firemonkey user style text sub-property

I created a style like this:

enter image description here

Then I create a TLayout inherited component named TLieu

My goal is to instantiate it at runtime and change: Image property, Title.Text property, Informations.Text property, Map, Infos, Link buttons

My code, but it's not working:

Procedure TF_Main.Button1Click(Sender: TObject);
var
  Lieu: TLieu;
begin
  Lieu := TLieu.Create(VSB_Place);
  Lieu.Name := 'Test';
  Lieu.StyleName := 'Lieu';
  Lieu.StylesData['Title.Text'] := 'My Title';
  // TLayout(Lieu).StylesData['Title.Text'] := 'My Title';  // this doesn't work either
end;

How can I change the sub properties of a custom user styled component at runtime?

Upvotes: 1

Views: 2455

Answers (2)

alitrun
alitrun

Reputation: 1218

Here is an example how I do it with ListBox Item

vItem.StylesData['descript'] := 'Description';
vItem.StylesData['details'] := 'Details text';
vItem.ImageIndex := 3;

In my case "descript" and "details" are TText. you can change names in StyleName property (in styles).

To change the image simply by specifying an image index for the ListBox Item (vItem.ImageIndex) in my example, the TGlyph image in style should have the name 'glyphstyle'.

// how to access to component in style:

vItem.NeedStyleLookup;
vItem.ApplyStyleLookup; // without this, FindStyleResource will return nil

vSwitch := vItem.FindStyleResource('switch') as TSwitch;
Assert(vSwitch <> nil);
vSwitch.IsChecked := vCampaign.Enable;
vSwitch.OnClick := DoOnClickSwitch;

Upvotes: 1

Dmitriy Nekrasov
Dmitriy Nekrasov

Reputation: 154

  1. TLieu must be inherited from TStyledControl
  2. Use Lieu.StyleLookup instead of Lieu.StyleName
  3. Before change StyleData call ApplyStyleLookup

Upvotes: 2

Related Questions