我写了一些代码,它用来自数据库的一些数据填充控件。如果控件直接放在窗口中,一切正常。但如何来填补控件,这是其他控件内部,如的TabControls,GroupBoxes等 我的代码如下所示:WPF - 填充数据控件,即在另一个控件内部
一些窗口:
private void LoadDataP()
{
if (ID.Length > 0)
{
if (baseButtons.LoadProcedureSelectName != string.Empty)
LoadData = SqlHelper.GetTable(baseButtons.LoadProcedureSelectName, new string[] { IdName, ID });
if (LoadData != null)
foreach (DataRow dr in LoadData.Rows)
{
SqlHelper.FillWindowControllsWithData(myGrid, dr);
}
}
}
有另一种方法class.They做主要工作:
public static void FillWindowControllsWithData(Grid windowGrid, DataRow dataRow)
{
foreach (Control ctrl in windowGrid.Children)
{
FillWindowControllsWithData(ctrl, dataRow);
}
}
public static void FillWindowControllsWithData(Control ctrl, DataRow dataRow)
{
if (ctrl.Name.IndexOf("db_") == 0)
{
if (ctrl is TextBox)
{
if (dataRow.Table.Columns.Contains(ctrl.Name.Substring(3)))
{
((TextBox)ctrl).Text = dataRow[ctrl.Name.Substring(3)].ToString();
}
}
} //end if
}
所以没有任何人知道如何填写texbox,这在一些组框或TabControl的数据,其中也有孩子..?
这可能是最好的,最简洁的解释MVVM为我所遇到的外行...非常好说 – Hardryv 2014-08-05 21:57:20