如何给用户创建动态数据
本文关键字:动态 数据 创建 给用户 | 更新日期: 2023-09-27 18:08:21
我用c#工作。Net 4.5.2, WPF, Win7.
我有一个a类的大名单。A类含有50~种不同的性质。我希望用户能够从类中选择2个属性,以便创建一个图形,其中第一个属性是X值,第二个属性是Y值。我已经创建了一个用户控件来创建图形,我只需要将它连接到Point.
UI有两个组合框,用户可以从列表中选择X属性和Y属性。
我想做一个大的开关情况下,以创建每个点,但我认为必须有一个更简单的方法来创建点的图形(LINQ也许??)
这里有一个示例(尚未测试)
List<A> yourData = new List<A>();
System.Reflection.PropertyInfo[] properties = typeof(A).GetProperties()
.Where(x => x.PropertyType == typeof(double)) //do a sanity check if property is double
.ToArray();
//the user has to choose which PropertyInfo has to be taken... make a combobox or similar and use properties as binding source
var propertyX = properties[3];
var propertyY = properties[4];
// create a list with the values
List<Point> points = new List<Point>();
foreach (A item in yourData)
{
Point newPoint = new Point();
newPoint.X =(double) propertyX.GetValue(item);
newPoint.Y = (double)propertyY.GetValue(item);
points.Add(newPoint);
}
//now do something with you extracted data and have fun