Tomtom
Tomtom

Reputation: 9394

Display Properties in UserControl

I have a class with a lot of properties like:

public class Person
{
   public string Firstname{get;set;}
   public string Lastname{get;set;}
   public string Address{get;set;}
   public string City{get;set;}
   public int ZIPCode{get;set;}
   public string Country{get;set;}
}

I want to create a UserControl, where I can pass an instance of Person and show for each Property a TextBlock with the PropertyName and a TextBox with the Property-Value.

My first idea would be to use a grid with two columns and 6 rows. But this is a lot of "work" to type this down.

Is there a "easy" way to achieve this? Can I do this with datatemplates or so?

Upvotes: 0

Views: 130

Answers (2)

David Brunelle
David Brunelle

Reputation: 6450

Use binding.

You could set the datacontext of the usercontrol to the object and bind to the object's properties.

or you can declare properties in the user control and use binding to bind each textblock to the user control property you just declare.

.

Your pick

Upvotes: 1

Kent Boogaart
Kent Boogaart

Reputation: 178780

One easy solution is the use a PropertyGrid, such as that in the extended WPF toolkit.

Upvotes: 2

Related Questions