I’m trying to figure out how to use a variable in code for databinding in a designer object. Example of what I’m trying to achieve: Say I have a string firstString and I set values to it in code. Now how do I make a TextBox to show this value through Databinding? I know I could just do TextBox.Text, but im looking for a Databinding way.
How about Generic lists? How would I populate a ListBox element in the designer with the objects from a list in the code by Databinding?
I went through this tutorial on how to link other designer objects to each other and some posts how to use whole classes as a DataContext. But I did not find an answer to this question in them.
Any help is appreciated.
WPF binding mechanism only support the properties. so the properties could be bind with other object but the variables are not allowed.
Element Name in binding is used to reference other controls, not variables in code behind. To reference variables in code behind, you need to assign that variable to a Control’s Data Context property.
XAML binding mec will only work with FirstString property not with the firststring variable.
also in C# concepts creating variables and accessing those outside is not recommended. the variables are usually designed for the local workout. where as if you want to share value of these variables the property making is the best and preferred solution.