How to bind a Control.Property to a property in the code-behind?

霸气de小男生 提交于 2019-12-13 03:25:10

问题


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"/>


来源:https://stackoverflow.com/questions/1734969/how-to-bind-a-control-property-to-a-property-in-the-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!