2013-10-28 34 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;