DataBindings do not appear to function properly
-
Dear Collegues, I thought I might try to use databindings to create a dialog that will enable a user to change some default colours. I can get the panel to show the colour stored in my class but I can't seem to get the class colour to update when the colour of the panel is changed. I have a databinding between the BackColor of a Panel control and the LineColour property of a ProjectPreferences Class. The details of each follow. When I show the dlgPrefs dialog, the pnlLineColour.BackColor is correct. After I change the pnlLineColour.BackColor using the ColorDialog, the m_pp.LineColour does not change to suit. Have I done this correctly or am I expecting too much? I am using VB.NET 2003 I have a class ProjectPrefences which holds the data. e.g. ProjectPreferences.LineColour:-
Public Property LineColour() As Color Get Return m_LineColour End Get Set(ByVal Value As Color) m_LineColour = Value End Set End Property
I call the dialog with the following code:-Dim MyPrefs As New ProjectPreferences Private Sub mnuPreferences_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPreferences.Click Dim dlg As New dlgPrefs dlg.PreferenceData = MyPrefs If dlg.ShowDialog() = DialogResult.OK Then MyPrefs = dlg.PreferenceData End If End Sub
The Dialog code is:-Public Class dlgPrefs Inherits System.Windows.Forms.Form Private m_pp As ProjectPreferences Public Property PreferenceData() As ProjectPreferences Get Return m_pp End Get Set(ByVal Value As ProjectPreferences) unBindData() m_pp = Value If Not m_pp Is Nothing Then BindData() End If End Set End Property ''' this routine handles a click in all of the colour panels Private Sub ColourPanel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pnlLineColour.Click, pnlElementColour.Click Dim cdlg As New ColorDialog cdlg.Color = sender.backcolor If cdlg.ShowDialog = DialogResult.OK Then sender.BackColor = cdlg.Color 'changes the panel.BackColor End If End Sub Private Sub BindData() 'pnlElementColour is a Panel on the dialog Me.pnlElementColour.DataBindings.Add("BackColor", m_pp, "ElementColour") Me.pnlLineColou