fbpx
Welcome, Guest
Username: Password: Remember me
Share your code snippets, screen shots etc. here
  • Page:
  • 1

TOPIC: WinForms and DataBinding

WinForms and DataBinding 1 year 2 months ago #1

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Away
  • Posts: 1401
  • Karma: 6
After working a lot with WPFs databinding, I have researched a bit about WinForms databinding, and I have to present a possible solution (again influenced from the years of VO programming :)).
WinForms databinding seems to be not so powerful as WPFs databinding, but simple solutions are possible also here. Please look at this peace of code:
using System.Windows.Forms        
using System.ComponentModel

class BaseView inherit Form
	
constructor()
	
return
	
method Use( oViewModel as INotifyPropertyChanged ) as void 
local oControls	as System.Windows.Forms.Control.ControlCollection
local oTextBox as TextBox

oControls := self:Controls
foreach oControl as Control in oControls 
  do case
  case oControl is TextBox
    oTextBox := ( TextBox ) oControl
    oTextBox:DataBindings:Add( "Text", oViewModel, oTextBox:Name )
  endcase
next
	
return
	
end class
Inherit your window class from this class (BaseView), and then build a ViewModel that implements the INotifyPropertyChanged interface. Assign this ViewModel then to your window in the Use() method, and the databinding will work:
oSampleView := SampleView{}
oSampleView:Use( SampleViewModel{} )
Application.Run( oSampleView )
Of course, the data in your ViewModel (or you can use the terminus "server") if you prefer :)) can come from any datasource, even from a DBF.

As always you can find a complete working sample application in XIDE export format attached to this message.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it
Attachments:

Please Log in or Create an account to join the conversation.

  • Page:
  • 1