为什么当我更改Form构造函数中的TextBox Text属性时,它不会影响绑定设置?
样品: 表单中的TextBox。 Text属性绑定到Settings字符串。 如果在Form构造函数中更改了TextBox.Text,则绑定的Setting不会更改其值。虽然在构造之外,它确实发生了变化(如预期的那样,因为它们是绑定的)。
我不知道绑定"魔法"工作,也许我在这里遗漏了一些东西。但这种行为至少是奇怪的。
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApplication1.Properties;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ChangeText();
}
private void button1_Click(object sender, EventArgs e)
{
ChangeText();
}
private void ChangeText()
{
//Settings.Default.TextBox1Text == "Original settings value"
textBox1.Text = "Changed";
//Call to this method inside Constructor:
// Settings.Default.TextBox1Text still equals its original value
//Settings.Default.TextBox1Text == "Original settings value"
//Call to this method outside Constructor:
// Settings.Default.TextBox1Text == "Changed"
}
}
}
namespace WindowsFormsApplication1
{
partial class Form1
{
/// <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.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", global::WindowsFormsApplication1.Properties.Settings.Default, "TextBox1Text", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
this.textBox1.Location = new System.Drawing.Point(12, 41);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = global::WindowsFormsApplication1.Properties.Settings.Default.TextBox1Text;
//
// button1
//
this.button1.Location = new System.Drawing.Point(157, 41);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
}
}