Save Time With This Quick LiveBindings Trick For Delphi And C++Builder
LiveBindings is a great technology built into Delphi and C++Builder for bypassing most of the code involved in a CRUD data cycle (Create, Read, Update and Delete Cycle). It can keep you out of massive amounts of technical debt. However, sometimes the standard LiveBindings method of binding some controls can be overly complex.
You can easily simplify your LiveBindings by using an invisible TEdit control. Basically you bind your field value to the TEdit and set the TEdit to Visible := False;. In the OnValidate event of the TEdit control you add code to update the REAL control you want to update (like a TTMSFMXSlider for example). In the OnChange event of the target control you send the updated value BACK to the invisible TEdit control and LiveBindings takes care of the rest. The TEdit control is bidirectional by default and will bind directly to a lot of field types. Whereas if you try to LiveBind directly to a Set, Enumeration, or SubRange it can be more difficult than it should be.
There are a lot of different ways to do this in LiveBindings but so far I am found this to be the easiest way. If you've got code snippets that do this easier (like using TLinkControlToField.AssigningValue or TMethodUtils.RegisterMethod) I'd like to hear about them so put them in the comments!
The demo I am providing over on GitHub shows how to bind a TFDMemTable field to a TEdit which updates a TTMSFMXSlider and is also bound to a TTMSFMXLiveGrid. You can change the switch, change the value in the TEdit, or change the value in the grid and all of the other values will automatically update. The code that pumps the switch change back to the TEdit is TLinkObservers.ControlChanged(SliderEdit); so you need to run this function to lock in the change. Here is the full code snippet from the demo but the real magic takes place in LiveBindings (the link to the demo source code is down below):
procedure TForm1.SliderEditValidate(Sender: TObject; var Text: string); begin case Text.ToInteger of 0: Slider.State := ssOn; 1: Slider.State := ssOff; end; end; procedure TForm1.SliderStateChanged(Sender: TObject); begin case Slider.State of ssOn: SliderEdit.Text := '0'; ssOff: SliderEdit.Text := '1'; end; TLinkObservers.ControlChanged(SliderEdit); FDMemTable1.Post; end;
Note: The demo uses TMS FMX UI Pack (don't leave home without it).
Please login first in order for you to submit comments
- Page :