2012-04-08 4 views
0

私は基底クラスと基底から継承する別のクラスを持っています。 は、基本クラスがDependencyPropertyに(たとえば "MyDPProperty")を有する:ウィンドウのコンストラクタでDataBindingは継承DependencyPropertyで機能しません

public int MyDPProperty 
{ 
    get { return (int)GetValue(MyDPPropertyProperty); } 
    set { SetValue(MyDPPropertyProperty, value); } 
} 
public static readonly DependencyProperty MyDPPropertyProperty =DependencyProperty.Register("MyDPProperty", typeof(int), typeof(ownerclass), new UIPropertyMetadata(0)); 

私が書きました:

SomeWpfWindow.DataContext = new ChildClass(); 

と私は私の窓のXAMLコードで:

<TextBox x:Name="txt" 
     Text="{Binding Path=MyDPProperty, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" /> 

バインディングは機能しません。後ろのコードでバインドすると動作しますが、

SomeWpfWindow.txt.SetBinding(TextBox.TextProperty 
         , new Binding("MyDPProperty") 
         { 
          Source = InstanceOfChildClass, 
          UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, 
          Mode = BindingMode.TwoWay 
         }); 
+0

DPプロパティの定義を投稿できますか? –

+0

これはコメントに投稿しないでください。 _Edit_質問。 –

+0

同様の問題:http://stackoverflow.com/questions/6071086/wpf-how-can-i-make-uielement-support-binding –

答えて

0

プロパティをコードの背後にバインドしています。あなたは、ウィンドウの名前(例えばNAME =「winName」)を与えるとの結合においてのElementNameを定義することができます。

x:Name="txt" Text="{Binding ElementName=winName, Path=MyDPProperty, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" /> 

あなたがStaticResourceを追加することができます例えば、他の方法は、あります。このリンクは次のように役立ちます。

http://blog.jayway.com/2011/05/17/bind-from-xaml-to-property-defined-in-code-behind/

関連する問題