在TListView中更改数字格式TObjectAppearance

问题描述

我将TListView与TFDQuery绑定在一起。数据之一映射到Item Detail,它基本上是一个数字。我想将数字格式化为##,## 0.00。看着Object Inspector,没有属性可以更改数据格式。

是否可以在TListView中更改数字的ItemAppearance?

下面的屏幕快照显示Object InspectorToogle DesignDebug视图:

screenshot showing the Object Inspector,Toogle Design,and Debug views

FMX程序:

unit Unit9;

interface

uses
  System.SysUtils,System.Types,System.UITypes,System.Classes,System.Variants,FMX.Types,FMX.Controls,FMX.Forms,FMX.Graphics,FMX.Dialogs,FMX.ListView.Types,FMX.ListView.Appearances,FMX.ListView.Adapters.Base,FireDAC.Stan.Intf,FireDAC.Stan.Option,FireDAC.Stan.Error,FireDAC.UI.Intf,FireDAC.Phys.Intf,FireDAC.Stan.Def,FireDAC.Stan.Pool,FireDAC.Stan.Async,FireDAC.Phys,FireDAC.Phys.sqlite,FireDAC.Phys.sqliteDef,FireDAC.Stan.ExprFuncs,FireDAC.FMXUI.Wait,FireDAC.Stan.Param,FireDAC.DatS,FireDAC.DApt.Intf,FireDAC.DApt,Data.Bind.EngExt,Fmx.Bind.DBEngExt,System.Rtti,System.Bindings.Outputs,Fmx.Bind.Editors,Data.Bind.Components,Data.Bind.DBScope,Data.DB,FireDAC.Comp.DataSet,FireDAC.Comp.Client,FMX.ListView,FMX.Controls.Presentation,FMX.StdCtrls,FMX.Edit;

type
  TForm9 = class(TForm)
    lbl1: TLabel;
    lsv1: TListView;
    con1: TFDConnection;
    qryLists: TFDQuery;
    bdr1: TBindSourceDB;
    bdl1: TBindingsList;
    tcf1: TLinkFillControlToField;
    lpfText: TLinkPropertyToField;
    qryInsert: TFDQuery;
    btn1: TButton;
    lnkcntrltfld1: TLinkControlToField;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form9: TForm9;

implementation

{$R *.fmx}

procedure TForm9.btn1Click(Sender: TObject);
begin
  qryInsert.ParamByName('id').AsInteger := 1;
  qryInsert.ParamByName('cur_datetime').AsDateTime := Now;
  qryInsert.ParamByName('name').Asstring := 'sample';
  qryInsert.ParamByName('size_cont').Asstring := 'size_cont';
  qryInsert.ParamByName('qty').AsFloat := 10;
  qryInsert.ParamByName('est_price').AsFloat := 1234.5;
  qryInsert.ParamByName('qty_price').Asstring := '10 x 1234.5';
  qryInsert.ParamByName('estimate').AsFloat := 10 * 1234.5;
  qryInsert.Execsql;
  qryLists.Close;
  qryLists.open();
end;

end.

FMX文件

object Form9: TForm9
  Left = 0
  Top = 0
  Caption = 'Form9'
  ClientHeight = 480
  ClientWidth = 308
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop]
  DesignerMasterStyle = 0
  object lbl1: TLabel
    Align = Top
    StyledSettings = [Family,Style,FontColor]
    Size.Width = 308.000000000000000000
    Size.Height = 33.000000000000000000
    Size.PlatformDefault = False
    TextSettings.Font.Size = 14.000000000000000000
    TextSettings.HorzAlign = Center
    Text = '1869'
    TabOrder = 0
  end
  object lsv1: TListView
    ItemAppearanceClassName = 'timageListItemBottomDetailAppearance'
    ItemEditAppearanceClassName = 'timageListItemBottomDetailShowCheckAppearance'
    HeaderAppearanceClassName = 'TListHeaderObjects'
    FooterappearanceClassName = 'TListHeaderObjects'
    Align = Client
    Size.Width = 308.000000000000000000
    Size.Height = 407.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 1
    ItemAppearanceObjects.ItemObjects.Text.Width = 201.000000000000000000
    ItemAppearanceObjects.ItemObjects.Text.Height = 20.000000000000000000
    ItemAppearanceObjects.ItemObjects.Text.PlaceOffset.Y = 1.000000000000000000
    ItemAppearanceObjects.ItemObjects.Detail.Width = 201.000000000000000000
    ItemAppearanceObjects.ItemObjects.Detail.Height = 20.000000000000000000
    ItemAppearanceObjects.ItemObjects.Detail.PlaceOffset.Y = 24.000000000000000000
  end
  object btn1: TButton
    Align = Bottom
    Position.Y = 440.000000000000000000
    Size.Width = 308.000000000000000000
    Size.Height = 40.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 7
    Text = 'btn1'
    OnClick = btn1Click
  end
  object con1: TFDConnection
    Params.Strings = (
      
        'Database=D:\My Project Files\5. my_projects_aws-rest\project_x_v' +
        '0\application\client\database\smartcart.s3db'
      'LockingMode=normal'
      'DriverID=sqlite')
    Connected = True
    LoginPrompt = False
    Left = 72
    Top = 24
  end
  object qryLists: TFDQuery
    Active = True
    Connection = con1
    sql.Strings = (
      'SELECT id,date_created,name,size_cont,qty_price,estimate'
      'FROM lists ORDER BY date_created DESC')
    Left = 112
    Top = 24
  end
  object bdr1: TBindSourceDB
    DataSet = qryLists
    ScopeMappings = <>
    Left = 152
    Top = 24
  end
  object bdl1: TBindingsList
    Methods = <>
    OutputConverters = <>
    Left = 20
    Top = 5
    object tcf1: TLinkFillControlToField
      Category = 'Quick Bindings'
      Control = lsv1
      Track = True
      FillDataSource = bdr1
      AutoFill = True
      FillExpressions = <
        item
          SourceMemberName = 'estimate'
          ControlMemberName = 'Detail'
        end>
      FillHeaderExpressions = <>
      FillBreakGroups = <>
    end
    object lpfText: TLinkPropertyToField
      Category = 'Quick Bindings'
      DataSource = bdr1
      FieldName = 'estimate'
      Component = lbl1
      ComponentProperty = 'Text'
    end
    object lnkcntrltfld1: TLinkControlToField
      Category = 'Quick Bindings'
      DataSource = bdr1
      FieldName = 'estimate'
      Track = False
    end
  end
  object qryInsert: TFDQuery
    Connection = con1
    sql.Strings = (
      'INSERT INTO lists (id,'
      '                   qty,est_price,estimate) '
      'VALUES (:id,:cur_datetime,:name,:size_cont,'
      '        :qty,:est_price,:qty_price,:estimate);')
    Left = 192
    Top = 24
    ParamData = <
      item
        Name = 'ID'
        DataType = ftInteger
        ParamType = ptInput
        Value = Null
      end
      item
        Name = 'CUR_DATETIME'
        DataType = ftDateTime
        ParamType = ptInput
        Value = Null
      end
      item
        Name = 'NAME'
        DataType = ftString
        ParamType = ptInput
        Value = Null
      end
      item
        Name = 'SIZE_CONT'
        DataType = ftString
        ParamType = ptInput
        Value = Null
      end
      item
        Name = 'QTY'
        DataType = ftFloat
        ParamType = ptInput
        Value = Null
      end
      item
        Name = 'EST_PRICE'
        DataType = ftFloat
        ParamType = ptInput
        Value = Null
      end
      item
        Name = 'QTY_PRICE'
        DataType = ftString
        ParamType = ptInput
        Value = Null
      end
      item
        Name = 'ESTIMATE'
        DataType = ftFloat
        ParamType = ptInput
        Value = Null
      end>
  end
end

解决方法

有2种方法适用于这种情况:

  1. 在TListView级别TLinkControlToField1 -> CustomFormat : "$ "+UpperCase(%s)上。这是最适用的方法,因为数字保持其数字格式,并且可以包含在计算中。您可能想知道Uppercase在这里做什么?只能将(%s)数据显示为字符串,并且无效。

  2. 使用TDataset字段的Display Format,格式为$ #,##0.00。通过这种方法,$将在您开始在计算中包含此数字时产生错误。

您应该检查此链接以获取更详细的说明https://stackoverflow.com/a/18819719/13810710

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...