DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: cuiqingbo
今日帖子: 20
在线用户: 9
导航: 论坛 -> 移动应用开发 斑竹:flyers,iamdream  
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2022/10/18 9:50:31
标题:
关于FireMonkey下ListView控件动态绑定数据请教 浏览:1347
加入我的收藏
楼主: 大家好,我现在ListView数据绑定,基本都是静态绑定的模式,但发现有时候静态绑定不够灵活,我想实现动态绑定模式,但试了几种方法,都不成功,可能对此的理解度是不够的,求各路神仙指点

比如:ItemAppearance模式下的Text1如何绑定BindSourceDB1上面的ALine字段?
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 janker (janker) ★☆☆☆☆ -
盒子活跃会员
2022/10/18 14:10:39
1楼: 打开静态绑定产生的TBingingList, 参考下里面的绑定表达式,比如TEdit绑定到一个域用的是TLinkControlToField,手动创建这个组件,填写几个必要的属性。

  lptft := TLinkControlToField.Create(Self);
  lptft.Control := edt1;
  lptft.DataSource := bsd1;
  lptft.FieldName := 'CompanyName';
  lptft.AutoActivate := True;

  fdqry1.Active := True;

更好的方法是写表达式,但是这个要时间学学
----------------------------------------------
-
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2022/10/20 8:49:32
2楼: 这几天试了好多方法,都没有成功,哪位大神能指点一下,谢谢了
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2022/10/20 8:51:16
3楼: 就像下面这个图所示,动态绑定该如何写呢
此帖子包含附件:
PNG 图像
大小:24.3K
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2022/10/20 12:19:58
4楼: how it should be?  in code? or by LiveBinding?

what you want to say with "Dinamic Binding"?

do you want change the "fields" binding in runtime?
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2022/10/20 14:50:49
5楼: TO:emailx45

我想通过代码的方式建立ListView与数据集的绑定,这样当我查询不同的数据时,可以给ListView的Text重新绑定字段,这样将相对灵活
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 zhyhero (zhyhero) ★☆☆☆☆ -
盒子活跃会员
2022/10/20 15:25:55
6楼: 对着当前做好Livebinding设置的From,鼠标右键->"View as text"
看看关于livebinding的部分都有什么,然后用代码照抄一遍试试。

例如:
(.fmx file)

object BindSourceDB1: TBindSourceDB
    DataSet = FDMemTable1
    ScopeMappings = <>
    Left = 352
    Top = 176
end
object BindingsList1: TBindingsList
    Methods = <>
    OutputConverters = <>
    Left = 348
    Top = 85
    object LinkGridToDataSourceBindSourceDB1: TLinkGridToDataSource
      Category = 'Quick Bindings'
      DataSource = BindSourceDB1
      GridControl = StringGrid1
      Columns = <>
    end
    object LinkListControlToField1: TLinkListControlToField
      Category = 'Quick Bindings'
      DataSource = BindSourceDB1
      Control = ListView1
      FillExpressions = <
        item
          SourceMemberName = 'ID'
          ControlMemberName = 'Text1'
        end
        item
          SourceMemberName = 'Name'
          ControlMemberName = 'Text2'
        end
        item
          SourceMemberName = 'Data'
          ControlMemberName = 'Text3'
        end>
      FillHeaderExpressions = <>
      FillBreakGroups = <>
    end
  end

然后写动态创建伪代码(fake codes):

BindSourceDB1:=TBindSourceDB.Create(?);
BindSourceDB1.DataSet:=FDMemtable1;

BindingsList1:=TBindingsList.Create(?);

LinkGridToDataSourceBindSourceDB1:=TLinkGridToDataSource.Create(? or BindingsList1);
LinkGridToDataSourceBindSourceDB1.Category = 'Quick Bindings';
LinkGridToDataSourceBindSourceDB1.DataSource = BindSourceDB1;
LinkGridToDataSourceBindSourceDB1.GridControl = StringGrid1;

LinkListControlToField1:=TLinkListControlToField.Create(? or BindingsList1);
LinkListControlToField1.Category = 'Quick Bindings';
LinkListControlToField1.DataSource = BindSourceDB1;
LinkListControlToField1.Control = ListView1;

LinkListControlToField1.FillExpressions.item[0].SourceMemberName = 'ID'
LinkListControlToField1.FillExpressions.item[0].ControlMemberName = 'Text1'
LinkListControlToField1.FillExpressions.item[1].SourceMemberName = 'Name'
LinkListControlToField1.FillExpressions.item[1].ControlMemberName = 'Text2'
LinkListControlToField1.FillExpressions.item[2].SourceMemberName = 'Data'
LinkListControlToField1.FillExpressions.item[2].ControlMemberName = 'Text3'

然后再想办法把上面的伪代码(fake code)改成正确(correct code)的,例如每个部分都需要怎么创建,初始化一类的。

希望能对你有所帮助。
----------------------------------------------
z@S7
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2022/10/20 17:29:36
7楼: TO:zhyhero

感谢

试了一下,还是不行
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2022/10/20 17:32:53
8楼: 我的代码写成了这样,运行报错
var
  LinkCR:TLinkListControlToField;
begin
  LinkCR:=TLinkListControlToField.Create(Self);

  CDS1.Close;
  CDS1.CommandText:='select * from PerFile';
  CDS1.Open;

  try
     LinkCR.DataSource:=BindSourceDB1;
     LinkCR.Category:= 'Quick Bindings';
     LinkCR.Control:=ListView1;

     LinkCR.FillExpressions.Items[1].SourceMemberName:='PerName';
     LinkCR.FillExpressions.Items[1].ControlMemberName:='Text1';

  finally
     LinkCR.DisposeOf;
  end;
此帖子包含附件:
JPEG 图像
大小:101.2K
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 pcplayer (pcplayer) ★☆☆☆☆ -
普通会员
2022/10/20 18:50:47
9楼: 5 楼,你的目的,可以换一种方式解决:

不同的数据,设计期就是不同的 ListView。

你的代码不需要去考虑动态更换绑定,你的代码直接更换 ListView 就好了。
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2022/10/20 21:15:12
10楼: Firstly, you should detach yourself from the way of procedurally coding the code by "Design Patterns".

Don't think of "LiveBinding" as a simple binding of a "DataSource" and a "DataSet" and a "Control" (like Gridxxxx). It's much more than that, and it shouldn't be seen in the same way.

The "LiveBinding" is a black box that uses the "Design Pattern Observer" as the basis of its concept, among other possible ones.

You can't think about creating something complex without knowing the fundamentals, because at some point, you'll have a hard time solving the side effects.

Keep in mind the following: whether you use "LiveBinding" in design-time or in code, the end result should be the same in the "LiveBinding" environment.

Honestly, this kind of situation is unusual and not very desirable. Purpose switching can lead to chaos within the system if you don't fully master the environment.

As already exemplified above, creating the "LiveBinding" environment by code is not very different from creating a data table with its fields and other necessary information in code, however, "LiveBinding" has particularities that differentiate it from the use of " DataSource + DataSet + Control".

In the end, you'll have an environment that works, practically, with text (only), and in each iteration of the control's properties, there are many conversions performed in the background, giving the impression that you are working with data of different types.

I advise you to first use the trivial as mentioned above.

Now, if you want to insist on this "dynamics" you are looking for, then understand that this will involve some side effects in the final software.

Anyway, I'll see if I can create something to exemplify in a simpler way... but without any promises ok?

hug
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2022/10/20 21:20:12
11楼: somes infos about LiveBinding

- LiveBinding does use of "SCOPE" interface
- LiveBinding does use of "CONVERSORS" interface
- LiveBinding does use of "EVENTS" interface
- LiveBinding does use of "MANAGER" class
- LiveBinding does use of "BINDINGS" class
- LiveBinding does use of "ASSOCIATIONS" class
etc... so much 

here just a sample in my tests to associate a "field" to "value" in LiveBinding environment...

 LBExpToPriceValues := TBindings.CreateManagedBinding( { }
    MyArrInputScope,          { }
    MyBindInputExpr,          { }
    MyArrOutputScope,          { }
    MyBindOutputExpr,          { }
    MyOutputConverter,          { }
    MyBindingEventRec,          { }
    MyBindingManager,          { }
    [MyBindingOptions]          { }
    );
  //

it's not so easy than a "Dataset + DataSource + Control"
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2022/10/20 21:21:52
12楼: in your code:

...
  LinkCR.FillExpressions.Items[1].SourceMemberName:='PerName';
  LinkCR.FillExpressions.Items[1].ControlMemberName:='Text1';
...

 Items[ x ]  === Exists in LiveBinding??

I think that not exists yet!!!

---> LinkCR.FillExpressions.Items.ADD....
------ List/Array/Strings are "0" based ... not "1" based! *by default
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 zhyhero (zhyhero) ★☆☆☆☆ -
盒子活跃会员
2022/10/20 22:31:25
13楼: 下面是部分结果,
没有动态创建StringGrid的列(Column)和Listview的项目模板(ItemAppearance),这两个实在不会。

var
    BindSourceDB2: TBindSourceDB;
    BindingsList2: TBindingsList;
    LinkGridToDataSourceBindSourceDB2: TLinkGridToDataSource;
    LinkListControlToFieldValue2: TLinkListControlToField;
    FormatExpressionItem: TFormatExpressionItem;

begin

  BindSourceDB2 := TBindSourceDB.Create(self);
  BindSourceDB2.DataSet := self.FDMemtable1;

  BindingsList2 := TBindingsList.Create(self);

  //StringGrid
  LinkGridToDataSourceBindSourceDB2 := TLinkGridToDataSource.Create(self.BindingsList2);
  LinkGridToDataSourceBindSourceDB2.DataSource := self.BindSourceDB2;
  LinkGridToDataSourceBindSourceDB2.GridControl := self.StringGrid_Test;
  LinkGridToDataSourceBindSourceDB2.Category := 'Code Bindings';
  LinkGridToDataSourceBindSourceDB2.AutoActivate := true;

  //ListView
  LinkListControlToFieldValue2 := TLinkListControlToField.Create(self.BindingsList2);

  FormatExpressionItem := LinkListControlToFieldValue2.FillExpressions.AddExpression;
  FormatExpressionItem.SourceMemberName := 'ID';
  FormatExpressionItem.ControlMemberName := 'Text1';

  FormatExpressionItem := LinkListControlToFieldValue2.FillExpressions.AddExpression;
  FormatExpressionItem.SourceMemberName := 'Name';
  FormatExpressionItem.ControlMemberName := 'Text2';

  FormatExpressionItem := LinkListControlToFieldValue2.FillExpressions.AddExpression;
  FormatExpressionItem.SourceMemberName := 'Value';
  FormatExpressionItem.ControlMemberName := 'Text3';

  LinkListControlToFieldValue2.Category := 'Quick Bindings';
  LinkListControlToFieldValue2.DataSource := BindSourceDB2;
  LinkListControlToFieldValue2.Control := ListView_Test;

  LinkListControlToFieldValue2.AutoActivate := true;
end;
----------------------------------------------
z@S7
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2022/10/21 8:34:19
14楼: To:emailx45,zhyhero

首先非常感谢两位的回复,特别是emailx45,之前就帮我解决了一些问题。

是的,从VCL转到FireMonkey有很多的思维没有转换过来,对一些新东西的原理理解也不够,然后就碰到各种问题点,现在关于FireMonkey的资源太少太少

LiveBinding在FireMonKey中的使用是相当普遍的,我也需要找找相关的资料进行系统性的学习一下才行

你们有好的推荐吗?
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 zhyhero (zhyhero) ★☆☆☆☆ -
盒子活跃会员
2022/10/21 9:31:52
15楼: 官网的文档
Demo
电子书等?
Delphi_GUI_Programming_with_FireMonkey.rar
----------------------------------------------
z@S7
作者:
男 hi100 (hi100) ▲▲▲▲△ -
普通会员
2022/10/21 9:44:47
16楼: https://serge-girard.developpez.com/tutoriels/Delphi/Livebindings/ListView/
----------------------------------------------
-
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2022/10/22 10:58:36
17楼: 谢谢15,16楼的分享,这一篇英文资料比较多,我研究一段时间,有时间我也会把它翻译成中文资料分享
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行101.5625毫秒 RSS