Hi all,
In the attached example I have simple business object MyTestBO with two properties Seconds (int) and Name (string).
MyTestBO implements INotifyPropertyChanged.
There is a simple WinForm that shows the properties of the business object.
There is also FinWorm.Timer set up for 1 second. On its Tick event it increments the Seconds property on the business object.
Data Source Update Mode for the textbox bindings is OnValidation.
Looks fine. Counter is OK.
If I try to edit the text in the Name text box on every Timer.Tick event it goes back to the original value.
I assume that Binding Source control reloads all properties regardless of what property was changed.
Is there a way to avoid reload of all properties when only one was changed?
Code:
public class MyTestBO : INotifyPropertyChanged
{
public string Name { get; set; }
int _seconds = 0;
public int Seconds
{
get { return _seconds; }
set
{
_seconds = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Seconds"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public partial class BugTest : Form
{
public BugTest()
{
InitializeComponent();
myTestBOBindingSource.DataSource = new MyTestBO() { Seconds = 0, Name = "Test Name" };
}
private void timer1_Tick(object sender, EventArgs e)
{
(myTestBOBindingSource.DataSource as MyTestBO).Seconds++;
}
}
<pre lang="x-c#"> //Designer generated code
partial class BugTest
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.myTestBOBindingSource = new System.Windows.Forms.BindingSource(this.components);
((System.ComponentModel.ISupportInitialize)(this.myTestBOBindingSource)).BeginInit();
this.SuspendLayout();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// textBox1
//
this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.myTestBOBindingSource, "Name", true));
this.textBox1.Location = new System.Drawing.Point(162, 59);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 7;
//
// textBox2
//
this.textBox2.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.myTestBOBindingSource, "Seconds", true));
this.textBox2.Location = new System.Drawing.Point(48, 59);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(52, 20);
this.textBox2.TabIndex = 8;
//
// myTestBOBindingSource
//
this.myTestBOBindingSource.DataSource = typeof(TestBug.MyTestBO);
//
// BugTest
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(311, 194);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "BugTest";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.myTestBOBindingSource)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.BindingSource myTestBOBindingSource;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
}