再谈 Delphi 的 LiveBindings 绑定界面元素和对象

前言:

本博客前面有文章描述过如何将一个对象的属性和界面元素(比如,Label1)绑定。那篇文章写得有点复杂。这里简化一下操作过程的描述。

Delphi 版本:

Delphi 10.4 社区版。

操作方法:

1. 假设有一个类定义:

TMyDev = class
  private
    FDevID: string;
    FDevName: string;
    FIsOnline: Integer;
    FIsSelected: Integer;
  public
    property DevID: string read FDevID write FDevID;
    property DevName: string read FDevName write FDevName;
    property IsOnline: Integer read FIsOnline write FIsOnline;
    property IsSelected: Integer read FIsSelected write FIsSelected;
  end;

2. 在 Form1 上面,有一个 Label1;

3. 有一个 Form1 的全局对象 FDev: TMyDev;

4. 拖一个 AdapterBindSource1 到 Form1 上面。拖一个 DataGeneratorAdapter1 到 Form1 上面。

5. 右键点击 DataGeneratorAdapter1 选择下拉菜单 Fields Editor;

6. 在 Fields Editor 窗口里面,增加字段。字段的名字和 TMyDEV 的属性名字相同,数据类型相同。

7. AdapterBindSource1 的 Adapter 的属性,在属性选择框里面,下拉,选择 DataGeneratorAdapter1。

8. 对 DataGeneratorAdapter1 和 AdapterBindSource1 的设置完成,鼠标右键点击 Form1 在下拉菜单里面选择 Bind Visually,IDE 底部会出现可视化绑定的窗口。

9. 在可视化绑定的窗口里面,找到 Label1,会显示它的 TEXT 属性;找到 AdapterBindSource1,会显示之前在 DataGeneratorAdapter1 里面添加的字段。点击 Label1 的 TEXT 拉线到  AdapterBindSource1 里面的某一个字段,这里我选择 DevName 字段。

10. 开始写代码:在 Form1 上面找到 AdapterBindSource1 选择它,然后在属性面板的 Events 事件页里面,找到事件(这里只有一个事件 OnCreateAdapter),双击这个事件,IDE 产生代码框架,代码如下:

procedure TForm1.AdapterBindSource1CreateAdapter(Sender: TObject;
  var ABindSourceAdapter: TBindSourceAdapter);
begin
  if not Assigned(FDev) then FDev := TMyDev.Create;

  FDev.FDevID := 'ID-893545';
  FDev.FDevName := 'Name - MyBLE';
  FDev.FIsOnline := 0;
  FDev.FIsSelected := 1;

  ABindSourceAdapter := TObjectBindSourceAdapter<TMyDev>.Create(AdapterBindSource1,
                                                               FDev, True);

  ABindSourceAdapter.AutoPost := False;

end;

11. 在 DELPHI IDE 里按 F9 运行程序,程序编译运行起来后,可以看到 Label1 显示的是上面的代码里面的 DevName 的值;

12. 增加一个按钮和一个 Edit1,运行期修改对象的属性值,看看 Label1 显示的内容是否跟随变化:

procedure TForm1.Button1Click(Sender: TObject);
begin
  FDev.DevName := Edit1.Text;
  AdapterBindSource1.Refresh; //修改完对象的值后,必须执行一次 Refresh 否则界面元素 Label1.Text 没有跟随变化。
end;

13. 拖一个 Edit2 到 Form1 上,进入可视化绑定界面(鼠标右键点击 Form1 下拉菜单选择 Bind Visually),在可视化绑定界面里面,找到 Edit2,鼠标左键点下它的 TEXT 属性,拉线到AdapterBindSource1 的 DevName 字段上,建立绑定连接。此时,Label1 的 Text 属性和 Edit2 的 Text  属性,都拉线连接到 AdapterBindSource1 的 DevName 字段上面。

14. 按 F9 编译运行,此时,Label1 和 Edit2 都显示相同的内容,都是 FDev.DevName 的内容。然后在 Edit2 里面输入一些新的内容,按回车,可以看到 Label1 显示的内容变成了 Edit2 里面输入的新的字符串。这里,实际上是 Edit2 的 Text 被用户输入改变后,回车就直接改变了绑定的 AdapterBindSource1 对应的 DevName 字段的值,然后绑定到这个字段的 Label1.Text 也就跟随改变了。

总结:

设计期在 IDE 里面可视化将对象的属性值绑定到界面元素的基本框架:

1. 自己用代码定义的类,属性值是 Public 的;

2. 拖一个 DataGeneratorAdapter1 到界面上,打开它的 Fields Editor 增加字段,字段名和数据类型和类的属性名相同。

3. 拖一个 AdapterBindSource1 到界面上,指定它的 Adapter 是 DataGeneratorAdapter1;

4. 打开设计期可视化绑定的界面,做了 2,3 两步,这时候就可以在这里看到 AdapterBindSource1 有字段了,用鼠标把字段和对应的界面元素(比如 Label1)拉线连接。

5. 增加 AdapterBindSource1.OnCreateAdapter 事件的代码,主要是要在这里为它创建运行期的 Adapter:

ABindSourceAdapter := TObjectBindSourceAdapter<TMyDev>.Create(AdapterBindSource1,
                                                               FDev, True);

6. 当然,在这行代码前面,需要先创建 FDev 对象。

7. 看起来,设计期的 DataGeneratorAdapter1 仅仅是用于设计期可视化拉线用。因为设计期没有运行代码,Delphi IDE 是无法知道我们自己定义的类以及这个类有哪些属性的。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
********************************************************************** Author: TMS Software Copyright ?1996-2014 E-mail: info@tmssoftware.com Web: http://www.tmssoftware.com ********************************************************************** TMS Pack for FireMonkey TMS Pack for FireMonkey contains components for use in user interfaces designed with the Embarcadero FireMonkey framework. The components have been designed from the ground up based on the core concepts of the FireMonkey framework: made up of styles, fully cross-platform, scalable and compatible with FireMonkey抯 effects, rotation, livebindings. Release 2.3.0.1: ----------------- Highly styleable cross-platform FireMonkey controls Support for Windows 32 bit, 64 bit, Mac OS X, iOS and Android Support for HTML formatted text, including hyperlinks in various parts of the components Built-in support for LiveBindings in TTMSFMXTableView and TTMSFMXTileList, allows to bind any item element to data Includes various demos and an extensive PDF developers guide Includes various helper controls (badge, button and html enabled text controls) that can be used separately as well Includes several Sample applications for the TTMSFMXGrid component History : --------- v1.0 : first release v1.0.0.1 Fixed : Issue with setting the focus on the form (Can be activated / deactivated with ShowActivated property) in TTMSFMXPopup Fixed : Issue with BitmapContainer assignment V1.1.0.0 New : Introducing TTMSFMXSpeedButton New : Introducing TTMSFMXHotSpotImage with separate editor Improved : TMSIOS Define Fixed : Issue with initial state when State is ssOn in TTMSFMXSlider Fixed : Issue with Opacity in HTML Drawing in TTMSFMXHTMLText v1.1.0.1 Fixed : Issue with parent in TTMSFMXPopup v1.1.0.2 Fixed : Issue with lfm file missing in package v1.1.0.3 New : Support for update 4 hotfix 1 Fixed : Issue with order of freeing objects in TTMSFMXTableView v1.1.0.4 Fixed : Issue with state changing with mouse out of bounds in TTMSFMXSlider Fixed : Issue with resizing detail view in TTMSFMXTableView v1.5.0.0 New : Components TTMSFMXGrid, TTMSFMXNavBar, TTMSFMXEdit and TTMSFMXEditBtn, TTMSFMXProgressBar New : Helper components: TTMSFMXGridPrintPreview, TTMSFMXFindDialog, TTMSFMXReplaceDialog, TTMSFMXExcelIO, TTMSFMXRTFIO. Improved : Performance for handling hover & click of hotspots in TTMSFMXHotSpotImage Fixed : Issue with mouse capturing in TTMSFMXTableView Fixed : Issue with alignment and resizing of detail view container when item has no detail view assigned in TTMSFMXTableview v1.6.0.0 New : XE3 Support New : OnSearchOpen / OnSearchClose called when swiping done or showing the filter with the ShowFilter property in TMSFMXTableView New : OnCanColumnDrag, OnCanRowDrag events added in TTMSFMXGrid New : OnColumnDragged, OnRowDragged events added in TTMSFMXGrid New : LiveBindings support in TTMSFMXGrid Fixed : Issue with Options.Mouse.ColumnDragging = false / Options.Mouse.RowDragging = false in TTMSFMXGrid Fixed : Issue with OnCanInsertRow/OnCanDeleteRow triggering in TTMSFMXGrid Fixed : Issue with selection on top when dragging in touch mode in TTMSFMXGrid Fixed : Access violation when touching outside the grid in touch mode in TTMSFMXGrid Fixed : Enabling touch mode in none selection mode in TTMSFMXGrid Fixed : Issue with OnClick / OnDblClick in instrumentation components Fixed : Issue with scrolling and selecting value in iOS in TTMSFMXSpinner Fixed : Issue with escape key not cancelling edit mode in TTMSFMXGrid v1.6.0.1 Fixed : Issue with double databinding registration in XE2 ios package v1.7.0.0 : New : added components TTMSFMXCalendar and TTMSFMXCalendarPicker New : Fixed cell single and range selection in TTMSFMXGrid New : Autosizing columns / rows on double-click in TTMSFMXGrid New : Column persistence in TTMSFMXGrid Improved : Data reset when toggling active in TTMSFMXScope Fixed : Issue with checkbox and radiobutton not rendering correctly on iOS project in TTMSFMXGrid Fixed : Issue with accessing and adding progressbar cells in TTMSFMXGrid Fixed : Repaint bug in XE3 in TTMSFMXTileList Fixed : ShowGroupGount implement in TTMSFMXGrid Fixed : GroupCountFormat implemented in TTMSFMXGrid Fixed : Issue with memoryleak in TTMSFMXLedBar Fixed : Access violation when clicking edit button in TTMSFMXTableView Fixed : Issue with OnDblClick not triggered in TTMSFMXTableView Fixed : Issue with popupmenu on background interfering with scrolling interaction in TTMSFMXTableView Fixed : Issue with selection persistence of items when scrolling in TTMSFMXTableView Fixed : Access violation Loading footer at designtime in TTMSFMXPanel v1.7.5.0 New: Capability to export multiple grids to a single RTF file in TTMSFMXGridRtfIO New : Options.Filtering.MultiColumn added to perform automatic multicolumn filtering in TTMSFMXGrid Fixed : Issue with Delphi XE3 compatibility in TTMSFMXCalendar Fixed : Issue with ApplyPlacement for plTop/plTopCenter in TTMSFMXPopup Fixed : Issue with sorting & filtering in TTMSFMXGrid Fixed : Issue with InsertRow on non-initialized grid in TTMSFMXGrid Fixed : Issue with XE3 Mac filtering in TTMSFMXGrid v1.7.5.1 Fixed : Issue with anchor detection when scrolling in TTMSFMXGrid Fixed : Issue with ccCount column calc in TTMSFMXGrid v1.7.5.2 New : Exposed functions CancelEdit / StopEdit in TTMSFMXGrid Fixed : Issue with text initialization in constructor in TTMSFMXGrid Fixed : Issue with default values for ArrowHeight and ArrowWidth in TTMSFMXPopup Fixed : Issue with focusing calendar in TTMSFMXCalendar Fixed : Issue with lookuplist in TTMSFMXEdit in XE3 Fixed : Issue with absoluteopacity in TTMSFMXLED v1.8.0.0 New : PDF Export component for Windows (QuickPDF), Mac and iOS v1.8.0.1 Fixed : Issue with absolute opacity in TTMSFMXBitmap Fixed : Issue with editing cell functionality while selecting all cells in TTMSFMXGrid v1.8.0.2 Fixed : Issue with peristing column states Fixed : Issue with printing DPI on different real and virtual printers v1.8.0.3 Fixed : Issue with dblclick in TTMSFMXGrid Fixed : Issue with dropdown window visible after editing in TTMSFMXGrid Fixed : Issue with wordwrapping and strikeout font style in html engine v1.8.0.4 : Fixed : Issue with debug message in TTMSFMXGrid v1.8.0.5 Fixed : Issue with dblclick focusing issue in TTMSFMXGrid Improved : comment popup in cell configurable in TTMSFMXGridCell v1.8.0.6 Improved : PDF Export engine v1.8.0.7 Package build options change v1.8.0.8 Fixed : Issue with text repainting at runtime in TTMSFMXHTMLText Fixed : Issue with text fill color initialization in TTMSFMXHTMLText Fixed : Repaint bug in XE3 in drag/drop tiles in TTMSFMXTileList Fixed : Issue with memoryleak when reparenting items in TTMSFMXTableView Fixed : Issue with hidden radio button checked property in TTMSFMXGrid v1.8.1.0 Fixed : Issue with clipboard on empty grid in TTMSFMXGrid New : Event OnTopLeftChanged event v1.8.1.1 Improved : block refreshing columns when destroying the component in TTMSFMXSpinner Fixed : Small issue in HTML Rendering engine for anchor rendering in TTMSFMXHTMLEngine Fixed : Issue with hidecolumn in TTMSFMXGrid v1.8.1.2 Issue with C++Builder OSX library paths not updated when installing v1.8.1.3 Issue compilation in FPC with TMSPlatforms constant v1.8.1.4 Improved : Added Event OnCellEditCancel in TTMSFMXGrid Improved : OnTileAnchorClick event exposed in TTMSFMXTileList Improved : Escape key handling in TTMSFMXCalendarPicker to cancel date selection Fixed : Issue with TMSFMXEdit signed float Fixed : Issue with dblclick in TTMSFMXEdit Fixed : Issue with dropdown access violation in TTMSFMXEdit Fixed : Access violation in TTMSFMXPopup v2.0.0.0 New : Syntax highlighting and editing component TTMSFMXMemo New : Persisted Columns collection in TTMSFMXGrid New : KeyBoard and Mouse Wheel handling support in TTMSFMXSpinner Improved : Escape to cancel date selection and return to previous selected date in TTMSFMXCalendar Fixed: Issue with autosizing and client-aligned captions v2.0.1.0 New : Pointer direction property in TTMSFMXBarButton Fixed : Issue with repainting header and footer text in TTMSFMXTableView Fixed : Selection changed notification to observer in TTMSFMXTableView Fixed : Issue with list read access in TTMSFMXNavBar Fixed : incorrect Gutter behavior when gutter is visible false or width = 0 in TTMSFMXMemo Fixed : Issue with XE2 FMI package memo registration v2.0.1.1 Fixed : Issue with height of tabs in TTMSFMXNavBar v2.0.2.0 New : OnButtonClick event in TTMSFMXEditBtn Fixed : Issue with fixed cell property persistence in TTMSFMXGrid Fixed : Issue with Excel font color import in TTMSFMXGrid Fixed : Issue with border width in new columns persistence collection in TTMSFMXGrid Fixed : Issue with bitmap assignment in TTMSFMXTableView Fixed : Issue with clearing cells in TTMSFMXGrid v2.0.2.1 Fixed : Issue with anchors & readonly cells in TTMSFMXGrid Fixed : Issue with handling Columns[x].ReadOnly Improved : Published events for Find and Replaced dialog in TTMSFMXMemo v2.1.0.0 New : XE4 support Fixed : Issue with memory leak in TTMSFMXGrid Fixed : Issue with triggering OnCursorChange in TTMSFMXMemo Fixed : Issue with UpdateMemo call when client-aligning Fixed : Issue with index out of bounds in empty grid connected to dataset v2.1.0.1 Improved : NextPage and PreviousPage methods in TTMSFMXGrid Fixed : Issue compiling demo's in trial version Fixed : Issue with LoadFromFile column widths in TTMSFMXGrid v2.1.0.2 Improved : GetTextHeight function in TTMSFMXHTMLText Fixed : Issue with iOS cell objects not being freed in TTMSFMXGrid Fixed : Issue with Scrolling when scrollbars are not visible in TTMSFMXGrid Fixed : Issue with readonly cells when using columns in TTMSFMXGrid Fixed : Issue accessing correct cell colors in TTMSFMXGrid Fixed : Issue with displaying DetailView in TTMSFMXTableView v2.1.0.3 Improved : Added Fixed Disjunct Row and Column Selection in TTMSFMXGrid Fixed : Issue with LiveBindings editing and row selection in TTMSFMXGrid Fixed : Replacement for TScrollBox issues at designtime / runtime. Fixed : Issue with Form Key handling in TTMSFMXEdit Fixed : Issue with designtime initialization of caption in TTMSFMXPanel Fixed : Issue with autocompletion popup in TTMSFMXMemo Fixed : Issue with font persistence in TTMSFMXMemo Fixed : Issue with ShowImage in TTMSFMXBarButton Fixed : Issue on iOS with categories in TTMSFMXTableView v2.1.0.4 Fixed : Issue with grid editing / inserting in LiveBindings in TTMSFMXGrid v2.1.0.5 Fixed : Memory leak with panel list in TTMSFMXNavBar Fixed : Access violation in TTMSFMXMemo v2.1.1.0 New : VisibleRowCount and VisibleColumnCount functionality in TTMSFMXGrid Fixed : Issue with disjunct selection and sorting in TTMSFMXGrid v2.1.1.2 New : LiveBinding support for Notes in TTMSFMXTileList v2.1.1.3 New : Published Anchors property Fixed : Issue inserting and deleting records in LiveBindings in TTMSFMXGrid v2.1.1.4 Fixed : Issue with RadioButtons in TTMSFMXGrid Fixed : Issue with Text position and width in TTMSFMXRotarySwitch v2.1.1.5 Fixed : Issue with rtf exporting on iOS in TTMSFMXGrid Fixed : Issue with ColumnStatesToString and column swapping in TTMSFMXGrid Fixed : Issue with lookup list on Mac in TTMSFMXEdit v2.1.1.6 Fixed : Issue with assign procedure collection item in TTMSFMXTableView Fixed : Issue with TTMSFMXPopup component on the form already exists v2.1.1.7 Improved: Assign() handling in TTMSFMXTableView Improved : conditionally use XOpenURL or XOpenFile for cell anchors in TTMSFMXGrid Fixed : Issue with Padding vs Margins in TTMSFMXPageSlider Fixed : Issue with comment resizing in TTMSFMXGrid Fixed : Issue with TableView resizing in TTMSFMXTableView Fixed : issue with column alignment settings for cell states different from normal in TTMSFMXGrid Fixed: Do not set TopMost = true in GetStyleObject, interferes with use on a popup in TTMSFMXEdit Fixed : Issue with initialization years in popup picker in TTMSFMXCalendarPicker Fixed : issue with setting Collaps = true at design time in TTMSFMXPanel Fixed : Issue with pressing ESC when date is empty in TTMSFMXCalendarPicker v2.2.0.0 New : XE5 support New : Android support New : Width and Height properties on lookup list in TTMSFMXEdit New : Redesign of TTMSFMXPopup to support iOS / Android Fixed : Issue with lookup autosize calculation in TTMSFMXEdit Fixed : Hints in TTMSFMXHTMLText and TTMSFMXBitmap Fixed : Issue with OnColumnSorted event not triggered in TTMSFMXGrid v2.2.0.1 Fixed : Issue with deleting rows in TTMSFMXGrid Fixed : Issue with date parsing in TTMSFMXCalendar Fixed : CSV parsing on iOS / Android in TTMSFMXGrid v2.2.1.0 New : OnMonthChanged and OnYearChanged events in TTMSFMXCalendar Fixed : Issue with money editing in TTMSFMXGrid v2.2.1.1 Fixed : Issue with disposing objects on iOS in TTMSFMXGrid Fixed : Issue with items return with incorrect CategoryID in TTMSFMXTableView v2.2.1.2 Fixed : Issue with column moving & column sizes in TTMSFMXGrid Fixed : Issue with save to file & linebreaks on iOS / Android in TTMSFMXGrid Fixed : Issue with column, row dragging out of the grid boundaries in TTMSFMXGrid Fixed : Issue with resizing in TTMSFMXTableView v2.2.1.3 Fixed : Issue with scrollbox and applystyle empty text initialization in TTMSFMXEdit Fixed : Issue with string conversion on iOS / Android in TTMSFMXGrid Fixed : Issue with column, row dragging out of the grid boundaries in TTMSFMXGrid Fixed : Issue with Livebindings in TTMSFMXCalendarPicker v2.2.1.4 Fixed : Issue with badge in TTMSFMXTileList Fixed : Issue with row sorting and objects in TTMSFMXGrid v2.2.2.0 Improved : Disjunct deselection of column, rows and cells in TTMSFMXGrid Fixed : Issue with horizontal scrolling and text trimming in TTMSFMXMemo Fixed : Issue with displaylist on XE5 in TTMSFMXEdit Fixed : Issue with assignment of item an OnItemClick in TTMSFMXTableView Fixed : Floating point division by zero in TTMSFMXPopup Fixed : Issue with OnCellEditDone event and focus changes v2.2.2.1 Fixed: Issue with Keyboard handling for ComboBox editor type in TTMSFMXGrid v2.2.2.2 Fixed : Issue with saving hotspot images in TTMSFMXHotSpotImage Fixed : Issue with memo find & replace dialog up and down search in TTMSFMXMemo Fixed : Issue with naming for led elements in TTMSFMX7SegLed Fixed : Issue with mouse enter/leave in TTMSFMXCalendar Fixed : Issue with column hiding and memory leak in sorting data in TTMSFMXGrid v2.2.2.3 Fixed : Issue with column/row hiding combinations in TTMSFMXGrid Fixed : Issue with Escape key in TTMSFMXGrid v2.2.2.4 Fixed : Issues with column/row handling in TTMSFMXGrid Fixed : Issue with calculation setpoints, needles and sections with minimumvalue > 0 in TTMSFMXLinearGauge v2.2.2.5 Improved: Signed numeric not being handled in GetInt/SetInt in TTMSFMXEdit Fixed: Issue with unhiding row combinations in TTMSFMXGrid v2.2.2.6 Improved : OnFormatCellDataExpression in TTMSFMXGrid Fixed : Issue with selection initialization in TTMSFMXGrid Fixed : Issue with cell creation in xls export v2.2.2.7 Fixed : Issue with using semi opaque hover, selected, down color in TTMSHotSpotImage Fixed : Issue with row insertion in TTMSFMXGrid v2.2.2.8 Fixed : Issue with grouping / ungrouping and row / column insertion changes v2.2.2.9 Improved : Clearing grid offset in TTMSFMXScope Fixed : Issue with initializing grid with default no. columns in TTMSFMXGrid Fixed : Issue with adding data without animation in TTMSFMXScope v2.3.0.0 New : XE6 Support v2.3.0.1 Fixed : XE6 Style compatibility issues Usage: ------ Use of TMS software components in applications requires a license. A license can be obtained by registration. A single developer license registration is available as well as a site license. With the purchase of one single developer license, one developer in the company is entitled to: - use of registered version that contains full source code and no limitations - free updates for a full version cycle - free email priority support & access to support newsgroups - discounts to purchases of other products With a site license, multiple developers in the company are entitled to: - use of registered version that contains full source code and no limitations - add additional developers at any time who make use of the components - free updates for a full version cycle - free email priority support & access to support newsgroups - discounts to purchases of other products Online order information can be found at: http://www.tmssoftware.com/go/?orders Note: ----- The components are also part of the TMS Component Pack bundle, see http://www.tmssoftware.com/go/?tmspack Help, hints, tips, bug reports: ------------------------------- Send any questions/remarks/suggestions to : help@tmssoftware.com Before contacting support about a possible issue with the component you encounter, make sure that you are using the latest version of the component. If a problem persists with the latest version, provide information about which Delphi or C++Builder version you are using as well as the operating system and if possible, steps to reproduce the problem you encounter. That will guarantee the fastest turnaround times for your support case.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值