本文介绍了FireMonkey XE5-动态绑定-TGrid-单元格文本匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows版Firemonkey桌面应用程序。我有一个 TGrid ,它通过可视的实时绑定 TBindSourceDB 组件进行填充。我希望某些列的文本右对齐,因为它们是数值。我已经尝试过:

I have a Firemonkey Desktop Application for Windows. I have a TGrid which I populate through a visual livebinding TBindSourceDB component. I want some columns to have text aligned to the right, as they are numerical values. I have tried through:


  • onPainting 事件

  • 通过 ColumnxRow

  • typecasting的数量获取 TTextCell 控件并在右侧设置 TextAlignt 属性

  • onPainting event
  • getting the TTextCell control by number of ColumnxRow
  • typecasting it and setting TextAlignt property to the right

这些措施均不能使文本对齐在右边。我尝试在运行时进行设置,但未成功,但获得了 TStyledControl 并将过程分配给 onApplyStyleLookup code> TTextCell 。

None of those measures align the text to the right. I have tried to set it at runtime, unsuccessfully though, getting the TStyledControl and assigning procedures to the onApplyStyleLookup of the TTextCell.

有任何想法吗?该应用程序正在运行,但没有任何反应。单元格文本仍保持左对齐。

Any ideas on it? The App runs, but nothing happens. The cell texts are still left aligned.

推荐答案

使用事件。

对于包含文本单元格的列,每个单独列的文本布局信息均从的网格属性。但是,分配是在事件触发之前执行的。

For columns containing text cells, the text layout information for each individual Column is assigned from the TextSettings property of the grid. However, the assignment is performed prior to the event firing.

最好和最简单的方法是,在绘制任何图形之前,仅通过类助手直接访问特定列的布局。发生。

The best and simplest way is to just directly access the layout for a specific column via a class helper before any drawing takes place.

设置属性设置为 False ,并粘贴以下代码:

Set the DefaultDrawing property of the grid to False and paste the following code:

interface
  type
    TColumnHelper = class helper for FMX.Grid.TColumn
        function getTextLayout: TTextLayout;
    end;

implementation

{ TColumnHelper }
function TColumnHelper.getTextLayout: TTextLayout;
begin
  Result := Self.FDrawLayout;
end;

{ OnDrawColumnCell }
procedure GridDrawColumnCell(Sender: TObject; const Canvas: TCanvas;
  const Column: TColumn; const Bounds: TRectF; const Row: Integer;
  const Value: TValue; const State: TGridDrawStates);
begin

  { change text layout info prior to default drawing }
  if Column.Header = 'Numerical Column' then
    Column.getTextLayout.HorizontalAlign := TTextAlign.Trailing
  else
    Column.getTextLayout.HorizontalAlign := TGrid(Sender).TextSettings.HorzAlign;

  { perform default drawing }
  TGrid(Sender).DefaultDrawColumnCell(Canvas, Column, Bounds, Row, Value, State);
end;

这篇关于FireMonkey XE5-动态绑定-TGrid-单元格文本匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:56