0

I have a base class and another class that inherits from the base. The base class has a DependencyProperty (say "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));

in Window's Constructor i wrote:

SomeWpfWindow.DataContext = new ChildClass();

and in my Window's XAML code i have:

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

Now, the binding does not work,Although when I bind it in code behind it works :

SomeWpfWindow.txt.SetBinding(TextBox.TextProperty
                        , new Binding("MyDPProperty")
                        {
                            Source = InstanceOfChildClass,
                            UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
                            Mode = BindingMode.TwoWay
                        });
Ali Adlavaran
  • 3,697
  • 2
  • 23
  • 47

1 Answers1

0

You are binding a property in code behind. you can give your window a name (for example Name="winName") and define the ElementName in the binding:

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

there are other ways, for example you can add a StaticResource. this link might help:

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

RoN
  • 41
  • 1
  • 2