P
PeterB
Hi!
I am having trouble with databinding between a custom class and a control. I
use the class as the interface between a textbox and a datasource such as a
database.
In my test application (source below), the idea is that if the user updates
the textbox it will be reflected in the object instantly, and if data is
read from a
datasource into the object (button1 is clicked) it will be reflected in the
controls instantly. The first is no problem. If I change the text in the
textbox the object is updated correctly, but if I change the object propery
value, the textbox text is NOT updated...
I solved this by iterating through all control's databinding lists and call
the underlying BindingManagerBase.ResumeBinding(); This works in the full
framework but ResumeBinding is not supported in the compact framework.
Has anyone any experience with this in the compact framework?
best regards,
Peter
Code:
public class testklass
{
private string m_test;
public testklass()
{
m_test = "First string";
}
public string test
{
get
{
return m_test;
}
set
{
m_test = value;
}
}
}
public class frmDataBinding : System.Windows.Forms.Form
{
private testklass tk;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public frmDataBinding()
{
InitializeComponent();
tk = new testklass();
textBox1.DataBindings.Add("text",tk,"test");
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 64);
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(96, 168);
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(88, 100);
this.textBox2.Text = "textBox2";
//
// Form1
//
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new frmDataBinding());
}
private void button1_Click(object sender, System.EventArgs e)
{
tk.test = "Second String";
foreach (Control ctrl in this.Controls)
{
foreach (Binding db in ctrl.DataBindings)
{
db.BindingManagerBase.ResumeBinding();
}
}
}
}
I am having trouble with databinding between a custom class and a control. I
use the class as the interface between a textbox and a datasource such as a
database.
In my test application (source below), the idea is that if the user updates
the textbox it will be reflected in the object instantly, and if data is
read from a
datasource into the object (button1 is clicked) it will be reflected in the
controls instantly. The first is no problem. If I change the text in the
textbox the object is updated correctly, but if I change the object propery
value, the textbox text is NOT updated...
I solved this by iterating through all control's databinding lists and call
the underlying BindingManagerBase.ResumeBinding(); This works in the full
framework but ResumeBinding is not supported in the compact framework.
Has anyone any experience with this in the compact framework?
best regards,
Peter
Code:
public class testklass
{
private string m_test;
public testklass()
{
m_test = "First string";
}
public string test
{
get
{
return m_test;
}
set
{
m_test = value;
}
}
}
public class frmDataBinding : System.Windows.Forms.Form
{
private testklass tk;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public frmDataBinding()
{
InitializeComponent();
tk = new testklass();
textBox1.DataBindings.Add("text",tk,"test");
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 64);
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(96, 168);
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(88, 100);
this.textBox2.Text = "textBox2";
//
// Form1
//
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new frmDataBinding());
}
private void button1_Click(object sender, System.EventArgs e)
{
tk.test = "Second String";
foreach (Control ctrl in this.Controls)
{
foreach (Binding db in ctrl.DataBindings)
{
db.BindingManagerBase.ResumeBinding();
}
}
}
}