【发布时间】:2019-04-06 05:02:08
【问题描述】:
这可能是一个非常基本的问题,但几个月以来我一直在努力解决这个问题。 我想创建一个简单的 UI 来创建我传递给 UI 构造函数的类的对象。 假设我有 2 个课程:
class Test1
{
public static List<Test1> objectList;
public string code;
public string name;
}
class Test2
{
public static List<Test2> objectList;
public string code;
public string name;
public int value;
}
(静态类将包含从该类创建的所有对象)
我想做的是创建一个代码,它将一个类作为变量(可能是一个泛型类?),并在此基础上根据类中可用的字段创建所有标签和文本框。 例如
public RegisterUI<T> ()
{
Grid grd = new Grid();
DataGrid dg = new DataGrid();
Button saveBtn = new Button();
//Binding the static list of objects to the DataGrid
dg.ItemSource = T.objectList;
//Adding the UI elemnts to the grid
grd.children.add(dg);
grd.children.add(saveBtn);
//Creating for each field in the Class T a lable based on its name and a combobox + binding
foreach(field in T)
{
Lable lbl = new Lable(field.getName);
ComboBox cbx = new ComboBox();
grd.children.add(lbl);
grd.children.add(cbx);
}
}
这甚至可能吗?我希望我不会对模型代码含糊其辞,您可以理解我的目标。 任何建议将不胜感激。非常感谢:)
【问题讨论】:
标签: c# wpf user-interface data-binding automation