Help to understand, I do not understand how to debug the code or why it does not work.

There is a window in which there is a class created in the constructor.

public class mainWindow : Window { private readonly Class1 _cls1 = new Class1(); public Class1 cls1 { get { return _cls1; } } } 

Class1 is a BaseClass derived

 public class Class1 : BaseClass { } 

BaseClass is a descendant of DepencesObject in which the DependencyProperty property is defined

 public class BaseClass : DepencesObject { public string Field { get{return(string)Getvalue(FieldProperty);} set{SetValue(FieldProperty, value);} } public static readonly DependencyProperty FieldProperty = DependencyProperty.register("Field", typeof(string), typeof(string)); } 

In the XAML form spelled:

 <TextBlock Text="{Binding cls1.Field}"/> 

But either the binding fails, or the data from the Field is not updated (at the time of form creation there "")

What am I wrong?

  • And what should there be at the time of creation? Where cls1 go? How should the UI know that cls1 updated? - Andrey NOP
  • @Andrey is recording when calling a certain method. Let's call it mainWindow.Load () {cls1.Field = "Text";} - pincher1519
  • cls1 = new ... - where? - Andrey NOP
  • And is the DataContext installed? - Andrey NOP
  • @Andrey, Updated the mainWindow code. The creation of the class takes place in the constructor. - pincher1519

1 answer 1

You have an error:

 DependencyProperty.register("Field", typeof(string), typeof(string)); 

This is wrong, you need

 DependencyProperty.Register("Field", typeof(string), typeof(BaseClass)); 

Do not create DependencyProperty manually, use the propdp .