■66506 / ) |
データバインディングでデータソースが更新されない |
□投稿者/ ゆうた (1回)-(2013/04/30(Tue) 02:43:03)
|
分類:[C#]
はじめまして、ゆうたと申します。よろしくお願いいたします。
環境はVirtualBox4.2.6上のWindows7SP1_64bitでVSE2012forWindowsDesktop、C#を使用しています。
フォーム上で値を変更して保存ボタンを押した際に
バインドしたオブジェクトのプロパティを変更するようにしたいと思っています。
しかし下記のソースコードだとtextBoxCl1F2をフォーム上で変更しても
this.class.Foo2が変更されず
textBoxA_Cl1F2に表示される値はコンストラクタで指定した20のままになってしまいます。
(実際に作成しているプログラムでは下記のソースコードの場合のnumericUpDownCl1F1への変更も
データソースに反映されないのですが、うまく再現できませんでした。)
デバッグしてみると最初のデータソースへの書き込み後に
textBoxCl1F2の値がclass1.Foo2の値に置き換わってしまいます。
これが原因でフォーム上で更新してもデータソースが変更できないようです。
textBoxCl1F2の値が変更されてしまう理由がよくわからず、
どのように対処すればよいのかわかりません。
どなたか原因がわからないでしょうか?
よろしくお願いいたします。
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication
{
public partial class Form1 : Form
{
Class1 class1;
public Form1()
{
InitializeComponent();
this.class1 = new Class1(10, 20);
this.numericUpDownCl1F1.DataBindings.Add("Value", this.class1, "Foo1", false, DataSourceUpdateMode.Never);
this.textBoxCl1F2.DataBindings.Add("Text", this.class1, "Foo2", false, DataSourceUpdateMode.Never);
this.numericUpDownCl2V1.DataBindings.Add("Value", this.class1.class2, "Val1", false, DataSourceUpdateMode.Never);
this.textBoxCl2V2.DataBindings.Add("Text", this.class1.class2, "Val2", false, DataSourceUpdateMode.Never);
}
private void button1_Click(object sender, EventArgs e)
{
this.numericUpDownCl1F1.DataBindings["Value"].WriteValue();
this.textBoxCl1F2.DataBindings["Text"].WriteValue();
this.numericUpDownCl2V1.DataBindings["Value"].WriteValue();
this.textBoxCl2V2.DataBindings["Text"].WriteValue();
this.numericUpDownA_Cl1F1.Value = this.class1.Foo1;
this.textBoxA_Cl1F2.Text = this.class1.Foo2.ToString();
this.numericUpDownA_Cl2V1.Value = this.class1.class2.Val1;
this.textBoxA_Cl2V2.Text = this.class1.class2.Val2.ToString();
}
}
}
namespace WindowsFormsApplication
{
class Class1
{
public int Foo1 { get; set; }
public int Foo2 { get; set; }
public Class2 class2 { get { return new Class2(); } }
public Class1(int arg1, int arg2)
{
this.Foo1 = arg1;
this.Foo2 = arg2;
}
}
}
namespace WindowsFormsApplication
{
class Class2
{
public int Val1 { get { return aa; } set { aa = value; } }
public int Val2 { get { return bb; } set { bb = value; } }
private static int aa;
private static int bb;
public Class2()
{
}
}
}
|
|