簡體   English   中英

具有活動綁定的firemonkey移動網格-在運行時更改TextCell文本顏色XE5

[英]firemonkey mobile grid with livebindings - changing TextCell text color at runtime XE5

我需要將我的貨幣單元格放在網格上以顯示本地貨幣符號,右對齊並用紅色顯示負數。

與類似的帖子不同,我使用實時綁定從數據集中填充TGrid。 其他解決方案建議從TStringCell為網格子類化“ TFinancialCell”,這在使用實時綁定時很困難。

使用Livebindings,綁定管理器控制網格列和單元格的創建,因此對綁定管理器(以及其他相關類)進行子類化可能既不實用也不雅致。

搞砸了一些我找到了可以解決我的問題的解決方案

通過使用數據集字段的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])”那樣訪問它們。 使用Grid OnPainting事件可在繪制單元格之前立即設置其格式。

右對齊是通過設置單元格在網格中的對齊方式來實現的。

通過使用樣式設置單元格文本顏色。 我們需要在應用程序StyleBook中創建一個“ textcellnegativestyle”。 除了“前景”畫筆顏色為紅色外,這與默認的“ textcellstyle”相同。 在桌面應用程序上,您可以在應用程序上放置一個TEdit,右鍵單擊它,然后選擇“ Edit Custom Style ...”,然后基於“ editstyle”將自定義樣式命名為“ textcellnegativestyle”,但只是將前筆刷顏色更改為紅色。

我的是一個移動應用程序,由於這個原因 ,“編輯自定義樣式”沒有出現在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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM