質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Q&A

2回答

2868閲覧

バインディングしたプロパティの値をtextboxに反映したいです。

meshkit

総合スコア72

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

0グッド

0クリップ

投稿2018/01/23 01:56

###前提・実現したいこと
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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

リアルタイムに反映されなくてもよいのであれば、以下のコードで実現可能です。

Form1のbutton1_Clickイベントに一行追記してください。

C#

1 private void button1_Click(object sender, EventArgs e) 2 { 3 Form2 form2 = new Form2(); 4 form2.ShowDialog(); 5 // ↓ 追加分 (DetaSourceの変更をGUIに通知するメソッドです。) 6 modeModelBindingSource.ResetBindings(false); 7 }

投稿2018/01/23 15:44

g_uo

総合スコア212

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

いまひとつ何がしたいのかよくわからないんですが、とりあえず ModeModel は INotifyPropertyChanged を実装してないのでバインドできないはずです。
これ以上聞かれても、結局どうなれば正解なのかがわからないので答えられません。

###追記
INotifyPropertyChanged を実装した ModeModel のサンプルです。

C#

1class ModeModel : INotifyPropertyChanged 2{ 3 public event PropertyChangedEventHandler PropertyChanged; 4 5 protected void OnPropertyChanged(string propertyName) 6 { 7 if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 8 } 9 10 private string title; 11 public string Title 12 { 13 get 14 { 15 return title; 16 } 17 set 18 { 19 if (title == value) return; 20 title = value; 21 OnPropertyChanged("Title"); 22 } 23 } 24}

投稿2018/01/23 14:27

編集2018/02/21 07:18
Zuishin

総合スコア28656

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

meshkit

2018/02/14 05:26

ありがとうございます。 ModeModeにINotifiPropertyChangedを実装する方法は、どうなりますか?
Zuishin

2018/02/14 05:30

public class ModeModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; } プロパティが変わった時にイベントを発行してください。
meshkit

2018/02/14 05:33

ありがとうございます。 これって、PropertyChangedのイベントハンドラは、ModeModelに実装するのですか? それともFormAとかFormBですか?
Zuishin

2018/02/14 05:36

バインドするということはプロパティが変化した時にバインドしたオブジェクトのプロパティを変化させるということです。 つまりどのプロパティが変化したのかを知らせる仕組みが必要です。 それが INotifyPropertyChanged です。 プロパティが変化したのを知らせたいオブジェクトにはすべて実装してください。
meshkit

2018/02/21 06:43

Form1になにを実装するのか、どうしてもわかりません。 Form1のFieldには public System.Windows.Forms.BindingSource modeModelBindingSource; があるのですが、これをどう使うのでしょう? this.modeModelBindingSource.DataSourceChanged += ModeModelBindingSource_DataSourceChanged; ではなく、 this.modeModelBindingSource.PropertyChanged は見当たらなくて困っています。
Zuishin

2018/02/21 06:47

ModeModel のプロパティの変化をテキストボックスに知らせたいのではないのですか? ModeModel で INotifyPropertyChanged を実装し、そのプロパティの変化でイベントを発行してください。 BindingSource はいじらなくて大丈夫です。
meshkit

2018/02/21 07:02

ModeModel で INotifyPropertyChanged を実装する。 public class ModeModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public string Title { get { return title; } set { title = value; } } private static string title = string.Empty; } でOK?
Zuishin

2018/02/21 07:03

set でプロパティが変化するのでそこでイベントを発行しなければなりません。
meshkit

2018/02/21 07:06

イベントを発行とは、どうすることでしょう?
meshkit

2018/02/21 07:10

public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(EventArgs e) { //ここになにか必要? }
Zuishin

2018/02/21 07:15

サンプルを追記しました。
meshkit

2018/02/21 07:29

ありがとうございます。 いま、Form1はこんなです。 public partial class Form1 : Form { ModeModel model = new ModeModel(); public Form1() { InitializeComponent(); model.PropertyChanged += OnPropertyChanged; //modeModelBindingSource.PropertyChanged += OnPropertyChanged;//<=これはどうするか。こっちにBindingしたいのに。 } private void OnPropertyChanged(object sender, PropertyChangedEventArgs e) { //ここに何を書く? //そもそもイベントが発火しない。どーん。不発弾。 Text = model.Title; } private void textBox1_TextChanged(object sender, EventArgs e) { model.Title = textBox1.Text; //↑なら、直接↓で済むのでは? Text = textBox1.Text; }
Zuishin

2018/02/21 07:36

急がずデータバインディングを基礎から学んでください。無茶苦茶です。 ここで説明することも不可能ではないかも知れませんが、それよりもっとわかりやすく説明したサイトがあるはずです。 建物を作るときには基礎を作ってから柱を立てるものですが、基礎も柱も無い状態で屋根を作ろうと四苦八苦しているように見えます。
Zuishin

2018/02/21 07:39

それと Qiita の私の記事を紹介したときも思ったのですが、字を読まなすぎです。 あの程度の説明が読めなければ何もできないんじゃないでしょうか。
meshkit

2018/02/21 07:41

できました。 public Form1() { InitializeComponent(); this.modeModelBindingSource.DataSource = new ModeModel(); ((ModeModel)this.modeModelBindingSource.DataSource).PropertyChanged += OnPropertyChanged; } private void OnPropertyChanged(object sender, PropertyChangedEventArgs e) { Text = ((ModeModel)this.modeModelBindingSource.DataSource).Title; } こういうことでしょうか。
Zuishin

2018/02/21 07:45

違いますが思い通りの動作になったのならそれでいいんじゃないでしょうか。
meshkit

2018/02/21 07:47

違うのでしょうか。違う気はしています。 ((ModeModel)this.modeModelBindingSource.DataSource).PropertyChanged += OnPropertyChanged; でcastは必要なのでしょうか。 これはデザイナーでは指定できないのでしょうか。
Zuishin

2018/02/21 07:49

私は基礎のないところに屋根を作る気はありません。 説明を書くなら読む人に向けて書きたいと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問