2013-10-28 33 views
1

我需要在网格上获取我的货币单元格以显示本地货币符号,右对齐并以红色显示负数。firemonkey移动网格与livebindings - 在运行时更改TextCell文本颜色XE5

与类似的帖子不同,我使用livebindings从数据集填充我的TGrid。 其他解决方案建议对来自TStringCell的“TFinancialCell”进行子分类,以便在使用livebindings时很困难。

使用Livebindings,绑定管理器控制网格列和单元格的创建,以便对绑定管理器(和其他相关类)进行子分类可能既不实用也不优雅。

+1

如果它解决了,你应该留下问题作为一个问题,然后添加答案。 o.o '' – EMBarbosa

回答

2

已经搞砸与此些我发现,回答我的问题

这些钱的符号是通过使用数据集字段的OnGetText事件返回格式化字符串获得的溶液:

procedure FDTableMyCurrFieldGetText(Sender: TField; var Text: string; 
    DisplayText: Boolean); 
begin 
    DisplayText := True; 
    Text := FloatToStrF(Sender.AsCurrency, ffCurrency, 18, 2); 
end; 

我可以在Grid OnPainting事件中做到这一点,但这样做可以为所有链接控件以及网格设置字段格式。我使用“发件人”而不是“FDTableMyCurrField”来引用字段,以便我可以将我的数据集中所有其他货币字段的OnGetText事件指向此方法。

格式化的其余部分在网格中完成。网格的单元格没有明确暴露,但您可以像这样“TTextCell(Grid1.Columns [I] .Children [J])”)获得它们。使用网格OnPainting事件在绘制之前立即格式化单元格。

右对齐是通过设置网格中的单元格对齐来实现的。

使用样式设置单元格文字颜色。 我们需要在我们的应用程序StyleBook中创建一个“textcellnegativestyle”。这将与默认的“textcellstyle”相同,只不过“前景”刷子颜色为红色。 在桌面应用程序上,您可以在应用程序中放置TEdit,右键单击它并选择“编辑自定义样式...”,然后根据“编辑样式”命名自定义样式“textcellnegativestyle”,但只需将前景刷子颜色更改为红。

Mine是一个移动应用程序,其中“编辑自定义样式”不出现在this reason的Delphi窗体编辑器弹出菜单选项中。 要添加自定义样式,您必须使用记事本或某些文本编辑器编辑.style文件(的副本)。

  1. 复制/粘贴“textcellstyle”对象
  2. 编辑粘贴的对象的名称为“textcellnegativestyle”
  3. 变化的“前景”画笔颜色为红色。
  4. 将编辑后的文件加载到您的应用程序StyleBook中。

这里是如何看起来在我的.style文件:

object TLayout 
    StyleName = 'textcellnegativestyle' 
    DesignVisible = False 
    Height = 50.000000000000000000 
    Width = 50.000000000000000000 
    object TLayout 
     StyleName = 'content' 
     Align = alContents 
     Locked = True 
     Height = 42.000000000000000000 
     Margins.Left = 4.000000000000000000 
     Margins.Top = 4.000000000000000000 
     Margins.Right = 4.000000000000000000 
     Margins.Bottom = 4.000000000000000000 
     Width = 42.000000000000000000 
    end 
    object TBrushObject 
     StyleName = 'foreground' 
     Brush.Color = claRed 
    end 
    object TBrushObject 
     StyleName = 'selection' 
     Brush.Color = x7F72B6E6 
    end 
    object TFontObject 
     StyleName = 'font' 
    end 
    end 

我使用Grid OnPainting事件来设置单元格对齐和风格。这里是我的工作解决方案:

procedure TFormMain.Grid1Painting(Sender: TObject; Canvas: TCanvas; 
    const ARect: TRectF); 
var 
    I, J: Integer; 
    T: TTextCell; 
begin 
    // my Column 0 is text, all other columns are money in this example 
    for I := 1 to Grid1.ColumnCount - 1 do 
    for J := 0 to Grid1.Columns[I].ChildrenCount- 1 do 
    begin 
     T := TTextCell(Grid1.Columns[I].Children[J]); 
     // set the Cell text alignment to right align 
     T.TextAlign := TTextAlign.taTrailing; 

     // test the Cell string for a negative value 
     if (T.Text[1] = '-') then 
     begin 
     // remove the leading minus sign 
     T.Text := Copy(T.Text, 2, Length(T.Text) - 1); 
     // set the font to red using the style 
     T.StyleLookup := 'textcellnegativestyle'; 
     end 
     else T.StyleLookup := 'textcellstyle'; 
    end; 
end;