###前提・実現したいこと
Visual Studio 2015 Proで、Bindingを作っています。
Windows Formアプリケーションです。
Form1, それを継承したForm2で、textboxをModeModelのプロパティにBindingしています。
Form1でtextboxに値を入れると、それをform2でも読めてます。
バインディングしたプロパティの値をtextboxに反映したいです。
###発生している問題・エラー
form2で値を入れると、プロパティには値が入るのですが、form1のtextboxには値を反映できません。
form1でプロパティを確認すると、値自体はform2で入れたものが入っています。
つまり表示できていないわけです。
changedのイベントハンドラを作ってみましたが呼び出されませんでした。
###該当のソースコード
C#
1Form1 2using System; 3using System.Windows.Forms; 4 5namespace Binding 6{ 7 public partial class Form1 : Form 8 { 9 public Form1() 10 { 11 InitializeComponent(); 12 this.modeModelBindingSource.DataSource = new ModeModel(); 13 //changedが必要? 14 this.modeModelBindingSource.DataSourceChanged += ModeModelBindingSource_DataSourceChanged; 15 } 16 17 protected virtual void ModeModelBindingSource_DataSourceChanged(object sender, EventArgs e) 18 { 19 //ここに何を書く? 20 //そもそもイベントが発火しない。どーん。不発弾。 21 //textBox1.Text = this.modeModelBindingSource.DataSource? 22 var workdata = this.modeModelBindingSource.DataSource; 23 } 24 25 private void button1_Click(object sender, EventArgs e) 26 { 27 Form2 form2 = new Form2(); 28 form2.ShowDialog(); 29 } 30 31 private void button2_Click(object sender, EventArgs e) 32 { 33 OnClick(); 34 } 35 protected virtual void OnClick() 36 { 37 //ここでBindingの値は確認できる。 38 var workdata = this.modeModelBindingSource.DataSource; 39 } 40 } 41} 42
C#
1Form1.designer 2namespace Binding 3{ 4 partial class Form1 5 { 6 /// <summary> 7 /// 必要なデザイナー変数です。 8 /// </summary> 9 private System.ComponentModel.IContainer components = null; 10 11 /// <summary> 12 /// 使用中のリソースをすべてクリーンアップします。 13 /// </summary> 14 /// <param name="disposing">マネージ リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param> 15 protected override void Dispose(bool disposing) 16 { 17 if (disposing && (components != null)) 18 { 19 components.Dispose(); 20 } 21 base.Dispose(disposing); 22 } 23 24 #region Windows フォーム デザイナーで生成されたコード 25 26 /// <summary> 27 /// デザイナー サポートに必要なメソッドです。このメソッドの内容を 28 /// コード エディターで変更しないでください。 29 /// </summary> 30 private void InitializeComponent() 31 { 32 this.components = new System.ComponentModel.Container(); 33 this.textBox1 = new System.Windows.Forms.TextBox(); 34 this.button1 = new System.Windows.Forms.Button(); 35 this.button2 = new System.Windows.Forms.Button(); 36 this.modeModelBindingSource = new System.Windows.Forms.BindingSource(this.components); 37 ((System.ComponentModel.ISupportInitialize)(this.modeModelBindingSource)).BeginInit(); 38 this.SuspendLayout(); 39 // 40 // textBox1 41 // 42 this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.modeModelBindingSource, "Title", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged)); 43 this.textBox1.Location = new System.Drawing.Point(78, 61); 44 this.textBox1.Name = "textBox1"; 45 this.textBox1.Size = new System.Drawing.Size(100, 19); 46 this.textBox1.TabIndex = 1; 47 // 48 // button1 49 // 50 this.button1.Location = new System.Drawing.Point(172, 111); 51 this.button1.Name = "button1"; 52 this.button1.Size = new System.Drawing.Size(75, 23); 53 this.button1.TabIndex = 2; 54 this.button1.Text = "openform2"; 55 this.button1.UseVisualStyleBackColor = true; 56 this.button1.Click += new System.EventHandler(this.button1_Click); 57 // 58 // button2 59 // 60 this.button2.Location = new System.Drawing.Point(172, 152); 61 this.button2.Name = "button2"; 62 this.button2.Size = new System.Drawing.Size(94, 23); 63 this.button2.TabIndex = 3; 64 this.button2.Text = "checkbinding"; 65 this.button2.UseVisualStyleBackColor = true; 66 this.button2.Click += new System.EventHandler(this.button2_Click); 67 // 68 // modeModelBindingSource 69 // 70 this.modeModelBindingSource.DataSource = typeof(Binding.ModeModel); 71 // 72 // Form1 73 // 74 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 75 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 76 this.ClientSize = new System.Drawing.Size(478, 230); 77 this.Controls.Add(this.button2); 78 this.Controls.Add(this.button1); 79 this.Controls.Add(this.textBox1); 80 this.Name = "Form1"; 81 this.Text = "Form1"; 82 ((System.ComponentModel.ISupportInitialize)(this.modeModelBindingSource)).EndInit(); 83 this.ResumeLayout(false); 84 this.PerformLayout(); 85 86 } 87 88 #endregion 89 private System.Windows.Forms.Button button1; 90 public System.Windows.Forms.BindingSource modeModelBindingSource; 91 private System.Windows.Forms.Button button2; 92 public System.Windows.Forms.TextBox textBox1; 93 } 94} 95 96
C#
1Form2 2namespace Binding 3{ 4 public partial class Form2 : Form1 5 { 6 7 public Form2() 8 { 9 InitializeComponent(); 10 } 11 12 protected override void OnClick() 13 { 14 var workdata = this.modeModelBindingSource.DataSource; 15 } 16 } 17} 18
Form2.Designer namespace Binding { public partial class Form2 : 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.SuspendLayout(); // // Form2 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(478, 230); this.Name = "Form2"; this.Text = "Form2"; this.ResumeLayout(false); this.PerformLayout(); } #endregion } }
C#
1ModeModel 2namespace Binding 3{ 4 public class ModeModel 5 { 6 public string Title { 7 get 8 { 9 return title; 10 } 11 set 12 { 13 title = value; 14 } 15 } 16 private static string title = string.Empty; 17 } 18} 19
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。