Reputation: 783
I'm often finding my self creating large objects with lots of properties for example customer data object including properties such as; First name Last name Address 1 Address 2 Address 3
Etc which I will have to write an interface for using labels and text boxes for each property giving them both id's which can get a bit tedious after a while.
Is there any kid of macro or source code which would do such a thing for me? I've searched on Google but I don't think I quite get the search for as I'm not entirely sure what I'm searching for.
Upvotes: 0
Views: 196
Reputation: 361
I am assuming that you wanted some way to generate the controls dynamically, try something like this
public MainWindow()
{
InitializeComponent();
CreateControls();
}
private void CreateControls()
{
var c = new Customer("SomeFirstName", "SomeLastName", "Something");
PropertyInfo[] pi = c.GetType().GetProperties();
Label lbl;
TextBox tb;
StackPanel sp = new StackPanel();
sp.Orientation = Orientation.Horizontal;
foreach (var p in pi)
{
MessageBox.Show(p.Name);
lbl = new Label();
lbl.Content = p.Name;
tb = new TextBox();
tb.Text = p.GetValue(c, null).ToString();
sp.Children.Add(lbl);
sp.Children.Add(tb);
}
//Replace MainGrid with any control you want these to be added.
MainGrid.Children.Add(sp);
}
}
public class Customer
{
public Customer(string first, string last, string title)
{
FirstName = first;
LastName = last;
Title = title;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public string Title { get; set; }
}
Upvotes: 0