2010-12-11 2 views
0

私はいくつかのテキストボックスをバインドして、私が作ったクラスのさまざまなフィールドを表示しようとしています:クラスオブジェクトにデータバインディングを使用できますか?

私は次のコードを試しました。

MyClass ClassObj = new MyClass(); 
    DataContext = ClassObj; 

    // Create a new binding 
    // Val1 is a variable of type String in MyClass class 
    Binding myNewBindDef = new Binding("Val1"); 

    myNewBindDef.Mode = BindingMode.TwoWay; 
    myNewBindDef.Source = ClassObj; 

    // txtBox1is a TextBlock object that is the binding target object 
    BindingOperations.SetBinding(txtBox1, TextBox.TextProperty, myNewBindDef); 

私はSystem.Windows.DataとSystem.ComponentModelのため使用して を追加した、とMyClassのはINotifyPropertyChangedのを実装しています。しかし、私は、アプリケーションを実行し、ClassObj.Val1の値を更新すると何も影響を与えないテキストボックスが空です。

私の欠点は何ですか?これを行うには良い方法がありますか?

おかげ

答えて

0

ああ、私は公的変数Val1を最初に作る必要があることが判明しました

1

INotifyPropertyChangedのに代わるものではMyClassのはDependencyObjectから継承させるとDependencyProperty.Register()関数を使用してVAL1ためのDependencyPropertyを作成することです。 DependencyPropertyは、MyClassのパブリック・レベルの静的メンバーに格納する必要があります。次に、Val1プロパティのgetterとsetterで、GetValue()とSetValue()を使用してDependencyPropertyにアクセスします。

関連する問題