Bind properties of a class at runtime (Create ViewModel at runtime)
I put this code on event TextChanged of the one TextBox you have, which is not efficient so change it as you wish. You can get all the classes in your namespace this way:
Type[] AllTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t =>
String.Equals(t.Namespace, **YOURNAMESPACENAME**, StringComparison.Ordinal)).ToArray();
Then you loop through the Types and find the class you want:
foreach (Type type in AllTypes)
{
if (type.Name == TargetClassName)
{
//Do Stuff
}
}
For example in your case //Do Stuff would be making a number of text boxes, so first we get the number of Properties in that Class:
//get number of properties
int PropertyCount = type.GetProperties().Length;
Then we make a list of text boxes and add them:
//make a list of textboxes for it
List<TextBox> TextBoxes = new List<TextBox>(PropertyCount);
for (int i = 0; i < PropertyCount; i++)
{
PropertyInfo ThisProperty = type.GetProperties()[i];
TextBox tb = new TextBox()
{
Text = ThisProperty.Name,
Name = "TB" + i,
Top = i * 32 + 44,
Left = 12,
Height = 20,
Width = 200,
};
//you can add a textchange event here for any of textboxes
tb.TextChanged += (s, ev) =>
{
//Do New Stuff
};
TextBoxes.Add(tb);
Controls.Add(TextBoxes[i]);
}
I hope I didn't misunderstand and this answers your question... This actually my first post here.