How to bind a Control.Property to a property in th

2019-09-18 10:12发布

问题:

I have a TextBox in my WPF window.

I have a property in the code behind called Text as well.

I want the text property to be bound to the Text property (i.e. when the value changes in the property the textbox value should also be updadated.

Thanks

回答1:

You have to set the DataContext of the TextBox control (or of any of its parents) to your window:

myTextBox.DataContext = this;

then in your XAML:

<TextBox Text={Binding Text} x:Name="myTextBox"/>