- PR -

ユーザーコントロールにデータバインド

1
投稿者投稿内容
Tog
ベテラン
会議室デビュー日: 2002/06/09
投稿数: 76
投稿日時: 2002-10-22 09:13
こんにちわ。
TextBox1とBotton1からなるUserControl作成しています。
このUserControlのパブリックプロパティとしてTextBox1.Textの文字を公開し、ここにメインフォームからのDataBindingを設定しようとしているのですが、データソースからコントロールにデータを表示させることはできるのですが、逆にコントロールからデータソースにデータがプッシュされないようです。

ユーザーコントロールの任意のプロパティにデータバインドを行いたい場合は何か変更を通知するコマンド等が必要なのでしょうか?
ヘルプを調べてみたのですが、わかりませんでした。
よろしくお願いいたします。
Tog
ベテラン
会議室デビュー日: 2002/06/09
投稿数: 76
投稿日時: 2002-10-24 16:48
色々と試しているのですが上手く出来ないでいます。

ユーザーコントロールはWindows.Forms.UserControlからの継承です。

呼び出し履歴をみているとどうもコントロールのValidatingイベントから
データバインダマネジャのParseが発生しているようです。
そこでユーザーコントロール内のTextBox.ValidatingイベントからベースクラスのOnValidatingを呼び出してみましたがやはりParseイベントが発生しないようです。

データバインダマネジャはコントロール内のプロパティの変更をどのように感知しているのでしょうか?

引き続き試してみます・・
NothingButXMLInfoSet
大ベテラン
会議室デビュー日: 2002/07/16
投稿数: 116
投稿日時: 2002-10-24 19:19
ユーザーコントロールで、内部テキストボックスのTextChangedイベントをControl.TextChangedへリルートします。そうしないと、ユーザーコントロールの変更がいつまでも"Dirty"なままでデータ更新が認められません。Validate系イベントはオプションですが、これをやると通常のテキストボックスと同じ動作になります。
コード:
public class UserControl1 : UserControl {
  private TextBox textBox1;
  private Button button1;
  private System.ComponentModel.Container components = null;

  public UserControl1() {
    InitializeComponent();
  }
  protected override void Dispose( bool disposing ) {
    if( disposing ) {
      if(components != null)
        components.Dispose();
    }
    base.Dispose( disposing );
  }

  private void InitializeComponent() {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    this.textBox1.Location = new System.Drawing.Point(0, 0);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "textBox1";
    this.textBox1.Validating += 
      new System.ComponentModel.CancelEventHandler(this.textBox1_Validating);
    this.textBox1.Validated += 
      new System.EventHandler(this.textBox1_Validated);
    this.textBox1.TextChanged += 
      new System.EventHandler(this.textBox1_TextChanged);

    this.button1.Location = new System.Drawing.Point(112, 0);
    this.button1.Name = "button1";
    this.button1.TabIndex = 1;
    this.button1.Text = "button1";

    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox1);
    this.Name = "UserControl1";
    this.Size = new System.Drawing.Size(192, 24);
    this.ResumeLayout(false);
  }

  private void textBox1_TextChanged(object sender, System.EventArgs e) {
    this.OnTextChanged(e);
  }

  private void textBox1_Validated(object sender, System.EventArgs e) {
    this.OnValidated(e);
    ((CurrencyManager)this.DataBindings[0].BindingManagerBase).Refresh();
  }

  private void textBox1_Validating(
    object sender, System.ComponentModel.CancelEventArgs e) {
      this.OnValidating(e);
  }

  public override string Text {
    get { return textBox1.Text; }
    set { textBox1.Text = value; }
  }
}


Validatedイベントの2行目はもっとまじめなコードにしないとダメでしょう。
Tog
ベテラン
会議室デビュー日: 2002/06/09
投稿数: 76
投稿日時: 2002-10-25 16:13
NothingBut.NETFXさんお返事ありがとうございます。
なるほど〜。TextChangedを呼び出すのですね。
上記のコードで正常に動作できるようになりました。

ドキュメントを調べてみたりしたのですがみつからずに困っていました。
ありがとうございました。
1

スキルアップ/キャリアアップ(JOB@IT)