Three TextBlocks In Cell
Three TextBlocks In Cell
This tab shows how to use a column of type TDataGridLayoutColumnEh to create cells with a complex structure.
Specifically, in this example, three rectangular blocks with text information inside are created in the cell:
The TDataGridLayoutColumnEh column is created in TDataGridEh at design-time.
TDataGridLayoutColumnEh.OnCreateDataCellContent event
procedure TfrSolGridThreeTextBlocksInCell.
DataGridLayoutColumnEh1CreateDataCellContent(Sender: TObject;
Params: TDataGridCreateDataCellContentParamsEh);
begin
with TLaStackPanelEh.CreateWith(Params.ContentParent) do
begin
Params.CellContent := RefSelf as TLaControlEh;
Orientation := TLaOrientationEh.Horizontal;
HorzAlignment := TLaHorzAlignmentEh.Center;
VertAlignment := TLaVertAlignmentEh.Center;
Font.Style := [TFontStyle.fsBold];
with TLaTextBlockEh.CreateWith(RefSelf) do
begin
Margins.Rect := TRect.Create(2, 2, 2, 2);
Padding.Rect := TRect.Create(4, 4, 4, 4);
Fill.Color := HotColors[1];
Fill.Kind := TBrushKind.Solid;
// Borders.RoundCorners := [TCorner.TopLeft, TCorner.TopRight];
Borders.RoundCorners := [TCorner.TopLeft, TCorner.BottomLeft];
Borders.RoundCornerRadius := 4;
// Borders.SetThicknesses(2, 2, 2, 2);
Borders.SetThicknesses(1, 1, 1, 1);
Borders.SetColors(TAlphaColorRec.Chocolate,
TAlphaColorRec.Chocolate,
TAlphaColorRec.Chocolate,
TAlphaColorRec.Chocolate);
Width := 48;
FieldName := 'ColVal1';
TextAlign := TTextAlign.Center;
Name := 'TextBlock1';
end;
with TLaTextBlockEh.CreateWith(RefSelf) do
begin
Margins.Rect := TRect.Create(2, 2, 2, 2);
Padding.Rect := TRect.Create(4, 4, 4, 4);
Fill.Color := HotColors[3];
Fill.Kind := TBrushKind.Solid;
Borders.RoundCorners := [];
Borders.RoundCornerRadius := 4;
Width := 48;
FieldName := 'ColVal2';
TextAlign := TTextAlign.Center;
Name := 'TextBlock2';
end;
with TLaTextBlockEh.CreateWith(RefSelf) do
begin
Margins.Rect := TRect.Create(2, 2, 2, 2);
Padding.Rect := TRect.Create(4, 4, 4, 4);
Fill.Color := HotColors[5];
Fill.Kind := TBrushKind.Solid;
Borders.RoundCorners := [TCorner.TopRight, TCorner.BottomRight];
Borders.RoundCornerRadius := 4;
Width := 48;
FieldName := 'ColVal3';
TextAlign := TTextAlign.Center;
Name := 'TextBlock3';
end;
end;
end;In the TDataGridLayoutColumnEh.OnCreateDataCellContent event handler, the cell structure is created using LaObject objects.
The first object created is the TLaStackPanelEh type. This is a panel that can arrange nested objects one after another horizontally or vertically.
In this example we set the horizontal orientation of the nested objects in the panel: Orientation := TLaOrientationEh.Horizontal;
Inside the code with TLaStackPanelEh.CreateWith(Params.ContentParent) do
three objects of type TLaTextBlockEh are created which will define the border and content of type text.
When we are inside the code with TLaStackPanelEh.CreateWith(Params.ContentParent) do, the RefSelf property points to the created object of type TLaObjectEh. We use this property to pass the Parent parameter to the constructor of the class TLaTextBlockEhwith TLaTextBlockEh.CreateWith(RefSelf) do.
This approach allows creating an unlimited depth of nested objects. Typically, the top-level objects are Panel components, which can contain other nested objects. At the lower levels, the objects are usually visual elements that present information, such as TLaTextBlockEh, TLaButtonEh, or TLaImageEh.
In the TLaTextBlockEh object we set:
Margins,PaddingFill- background fill colorBorders- borderBorders.RoundCorners- sides of the rectangular borderBorders.RoundCornerRadius- radius of rounding of corners of borderBorders.SetThicknesses- border thicknessBorders.SetColors- border colorsWidth- object width. If not specified, the width will be determined based on the text width.FieldName- field in the DataSet that will be used to fill theTextproperty. IfFieldNameis not specified, then the text must be written manually in theOnDataCellInitContentevent.TextAlign- horizontal text alignment.Name- Object name. The object name is needed so that it can be found in theOnDataCellInitContentevent.
Three objects of type TLaTextBlockEh are created in TLaStackPanelEh sequentially and will be displayed one after another from left to right.
TDataGridLayoutColumnEh.OnDataCellInitContent event
If in LaObjects only the text changed depending on the current entry, then writing an OnDataCellInitContent event handler would not be required.
But in our example we also set the background color of the rectangular areas.
We write the TDataGridColumnEh.OnDataCellInitContent event handler to set the color of the sections depending on the value of the numeric fields.
procedure TfrSolGridThreeTextBlocksInCell.
DataGridLayoutColumnEh1DataCellInitContent(
Sender: TObject;
Params: TDataGridInitDataCellContentParamsEh);
var
TextBlock: TLaTextBlockEh;
DataContext: IDataContextEh;
FieldValue: TValue;
begin
if Params.CellContent = nil then Exit;
TextBlock := Params.CellContent.GetElementByName('TextBlock1') as TLaTextBlockEh;
DataContext := TextBlock.DataContext;
if DataContext <> nil then
begin
FieldValue := DataContext.GetFieldValue('ColVal1');
TextBlock.Fill.Color := GetHotColor(Double((FieldValue.AsExtended - 50) * 2.5));
// TextBlock.Fill.Color := TAlphaColorRec.Null;
end;
TextBlock := Params.CellContent.GetElementByName('TextBlock2') as TLaTextBlockEh;
DataContext := TextBlock.DataContext;
if DataContext <> nil then
begin
FieldValue := DataContext.GetFieldValue('ColVal2');
TextBlock.Fill.Color := GetHotColor(Double((FieldValue.AsExtended - 50) * 2.5));
end;
TextBlock := Params.CellContent.GetElementByName('TextBlock3') as TLaTextBlockEh;
DataContext := TextBlock.DataContext;
if DataContext <> nil then
begin
FieldValue := DataContext.GetFieldValue('ColVal3');
TextBlock.Fill.Color := GetHotColor(Double((FieldValue.AsExtended - 50) * 2.5));
end;
end;Using the codeTextBlock := Params.CellContent.GetElementByName('TextBlock1') as TLaTextBlockEh;
we find an object named 'TextBlock1' in the cell's content tree.
The TextBlock.DataContext property points to the current TTableRowLinkEh record inside the TDataSetTableLinkEh class, which in turn receives values from the DataSet assigned to the TDataGridEh.DataSource property.
Methodfunction TfrSolGridThreeTextBlocksInCell.GetHotColor(Percent: Double): TAlphaColor;
returns a color in the range from green to red depending on the value of Percent.
As a result, the grid cells will look like this:
