0

いくつかのコントロールを持つフォームがあります。「textBoxOtherRelationship」が無効で、テキストが string.empty に設定されている状況があります。しかし、別のコントロールに移動してタブアウトすると、データが再び表示されますが、コントロールは無効のままです。

textBoxOtherRelationship.DataBindings.Add(new Binding("Text", _binder, "RelationshipNotes"));


  private void ComboBoxRelationShipSelectedValueChanged(object sender, EventArgs e)
        {
            if ((Constants.Relationship)comboBoxRelationShip.SelectedItem.DataValue == Constants.Relationship.Other)
            {
                textBoxOtherRelationship.Enabled = true;
                if (_formMode != ActionMode.ReadOnly)
                {
                    textBoxFirstName.BackColor = Color.White;
                }
            }
            else
            {
                textBoxOtherRelationship.Enabled = false;
                _model.RelationshipNotes = null;
                textBoxOtherRelationship.Text = string.Empty;
                if (_formMode != ActionMode.ReadOnly)
                {
                    textBoxFirstName.BackColor = Color.LightYellow;
                }
            }
        }
4

2 に答える 2

1

うーん..だから私はここにこの行を見ます:

textBoxOtherRelationship.DataBindings.Add(
  new Binding("Text", _binder, "RelationshipNotes"));

Textこれは、 textBoxOtherRelationship のプロパティと datasource の "RelationshipNotes" というプロパティの間にバインディングが設定されていることを示しています_binder

偉大な。

ですから、双方向バインディングは問題なく機能し、コントロールに何かを入力するtextBoxOtherRelationshipと、そのコントロールがフォーカスを失うと、基になる RelationshipNotes プロパティも更新されると思いますよね?

あなたのコードを見ると、Textプロパティをに設定したときに基になるデータソースが更新されているとは思いません。string.Emptyこれは、通常、テキストボックスがフォーカスを失い、コントロールを無効にするまで発生しないためです。

追加する場合:

textBoxOtherRelationship.DataBindings[0].WriteValue();

値をその文字列に設定した後。string.Emptyデータバインディングは更新するものがあることを認識するため、空の値がデータソースに保存されます。プログラム的には、そうではありません。

次の行があることがわかります。

            textBoxOtherRelationship.Enabled = false;
            _model.RelationshipNotes = null; <<<----------------------
            textBoxOtherRelationship.Text = string.Empty;

_model.RelationshipNotes最終的にそのテキストボックスにバインドされるはずのものは何ですか?

于 2012-08-22T20:38:13.120 に答える
1

SelectedIndexChanged イベントは、コントロールがフォーカスを失うまでデータバインディングをコミットしないため、簡単な修正は、イベントに最初に値を書き込むことです。

private void ComboBoxRelationShipSelectedValueChanged(object sender, EventArgs e)
{
  if (comboBoxRelationShip.DataBindings.Count > 0) {
    comboBoxRelationShip.DataBindings[0].WriteValue();

    if ((Constants.Relationship)comboBoxRelationShip.SelectedItem.DataValue ==
                                                 Constants.Relationship.Other) {
      textBoxOtherRelationship.Enabled = true;
      if (_formMode != ActionMode.ReadOnly) {
        textBoxFirstName.BackColor = Color.White;
      }
    } else {
      textBoxOtherRelationship.Enabled = false;
      _model.RelationshipNotes = null;
      textBoxOtherRelationship.Text = string.Empty;
      if (_formMode != ActionMode.ReadOnly) {
        textBoxFirstName.BackColor = Color.LightYellow;
      }
    }
  }
}
于 2012-08-22T22:27:51.303 に答える