在UserControl中显示属性

时间:2013-05-03 19:09:31

标签: c# wpf class

我有一个包含很多属性的类:

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;}
}

我想创建一个UserControl,我可以在其中传递Person的实例,并为每个Property显示一个带有PropertyName的TextBlock和一个带有Property-Value的TextBox。

我的第一个想法是使用一个包含两列和六行的网格。但这是很多“工作”来打字。

有一种“简单”的方法来实现这一目标吗?我可以用datatemplates这样做吗?

2 个答案:

答案 0 :(得分:2)

一个简单的解决方案是使用PropertyGrid,例如extended WPF toolkit中的{。}}。

答案 1 :(得分:1)

使用绑定。

您可以将usercontrol的datacontext设置为该对象并绑定到该对象的属性。

或者您可以在用户控件中声明属性并使用绑定将每个文本块绑定到您刚刚声明的用户控件属性。

你的选择

相关问题