delphi中的奇怪行为xe CustomListBoxItem

时间:2014-05-15 11:03:30

标签: delphi delphi-xe listboxitem delphi-xe6

我在Delphi xe6中创建了一个自定义ListBoxItem,(基于@MikeSutton在这篇文章中回答What control should I use to create this UI in Delphi Firemonkey

我有2 TNumberBox和2 TLabels。 这是我的自定义列表框项

TListBoxItemMatchBet = class(TListBoxItem)
private
            ....
            //some other methods and properties
    fLeftValue: integer;
    procedure setLeftValue(const Value: integer);
    procedure setLeftValueStyle();
    procedure LeftValueChange(Sender: Tobject);

protected
    procedure ApplyStyle; override;
published
    property Text: string read fText write setText;
    property LeftValue: integer read fLeftValue write setLeftValue;
    property RightValue: integer read fRightValue write setRightValue;

end;

procedure TListBoxItemMatchBet.setLeftValue(const Value: integer);
begin
    fLeftValue := Value;
    setLeftValueStyle();

end;

procedure TListBoxItemMatchBet.setLeftValueStyle;
var
    O: TFMXObject;
begin
    O := FindStyleResource('nmbLeft'); // StyleName of the item
    if O is TNumberBox then
    begin
        TNumberBox(O).ValueType := TNumValueType.Integer;
        TNumberBox(O).Value := fLeftValue;
        TNumberBox(O).OnChange := LeftValueChange;
    end;

end;
procedure TListBoxItemMatchBet.ApplyStyle;
begin
    inherited;
    setTextStyle();
    setLeftValueStyle();
    setRightValueStyle();
end;

procedure TListBoxItemMatchBet.LeftValueChange(Sender: Tobject);
begin
    fLeftValue := round((Sender as TNumberBox).Value);
end;

每件事情都没问题,除非我的列表框中有很多(大约20个)项目并且我向上滚动,当我向下滚动数字框的值将更改为其他记录时(例如当我滚动项目时值为50)它的价值会变成其他东西,比如10和50会转到其他列表框项目。)

此行为适用于Android和Iphone模拟器。

这是一些屏幕截图。

Setting Values

设置值(右上角)


Scroll Up

向上滚动


Scroll Down

向下滚动

价值消失

1 个答案:

答案 0 :(得分:1)

经过几天的努力,我找到了解决方案:

只需在样式簿中创建所需的样式,然后将项目添加到ListBoxItem

注意从TListBoxItem实例化

Itemx := TListBoxItem.Create(self);
Itemx.StyleLookup := 'listBoxItemNumericEditable';
Itemx.Text := 'A Title';

这是诀窍

Itemx.StylesData['nmbLeft.Value'] := 50;

你甚至可以像这样添加事件处理程序

  Itemx.StylesData['nmbLeft.OnChange'] := TValue.From<TNotifyEvent>(DoNumberChange); 
相关问题