0

基本クラスと、基本から継承する別のクラスがあります。基本クラスにはDependencyProperty(「MyDPProperty」など)があります。

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));

Windowのコンストラクターで私は書いた:

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
                        });
4

1 に答える 1

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/

于 2012-04-09T07:04:51.127 に答える