8

私はWinforms Databindingを使用し、基本クラスが実装する派生クラスを持っていますIPropertychanged:

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName) {
        var handler = this.PropertyChanged;
        if (handler != null) {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

各プロパティセッターは以下を呼び出します。

    protected void SetField<T>(ref T field, T value, string propertyName) {
        if (!EqualityComparer<T>.Default.Equals(field, value)) {
            field = value;
            IsDirty = true;
            this.RaisePropertyChanged(propertyName);
        }
    }

典型的な Propertysetter:

    public String LocalizationItemId {
        get {
            return _localizationItemId;
        }
        set {
             SetField(ref _localizationItemId, value, "LocalizationItemId");
        }
    }

プロパティをテキストボックスにバインドする方法

        private DerivedEntity derivedEntity
        TextBoxDerivedEntity.DataBindings.Add("Text", derivedEntity, "Probenname");

プログラムでテキストをテキスト ボックスに割り当てると、テキスト ボックスに表示されません。しかし、テキストボックスを手動で編集できます。

4

4 に答える 4

12

答えるには遅すぎることはわかっていますが、バインディングの値を変更する必要があるときにイベントを設定すると、この問題は解決できます。プロパティ値の変更イベントに設定すると、問題は解決します。この方法でこれを行うことができます

textBox.DataBindings.Add("textBoxProperty", entity, "entityProperty", true, DataSourceUpdateMode.OnPropertyChanged);
于 2014-12-12T05:30:23.070 に答える
8

TextBox Validated イベントでバインディング ソースが更新されます。TextBox 検証済みイベントは、ユーザーが TextBox を編集し、フォーカスを他のコントロールに変更したときに呼び出されます。プログラムで TextBox テキストを変更しているため、TextBox はテキストが変更されたことを認識しないため、検証が呼び出されず、バインディングが更新されないため、バインディングを手動で更新する必要があります。

バインディングの初期化:

var entity;
textBox.DataBindings.Add("textBoxProperty", entity, "entityProperty");

TextBox.Text を変更します。

textBox.Text = "SOME_VALUE";

バインディングを手動で更新します。

textBox.DataBindings["textBoxProperty"].WriteValue();

Binding.WriteValue() は、コントロールから値を読み取り、それに応じてエンティティを更新します。MSDNで WriteValue について読むことができます。

于 2013-10-19T17:24:02.537 に答える
1

サブスクライバが初期化されていません。すなわち

private DerivedEntity derivedEntity
TextBoxDerivedEntity.DataBindings.Add("Text", derivedEntity, "Probenname");

derivedEntity無効です。

初期化すれば大丈夫です。

于 2016-02-12T18:02:41.603 に答える