【发布时间】:2012-07-16 08:49:18
【问题描述】:
我在 C#,WPF 中有以下代码:
base.DataContext = new DataTemplate[]
{
new DataTemplate
{
lblText = "First",
txtBoxContent = ""
},
new DataTemplate
{
lblText = "Second",
txtBoxContent = "Something"
}
};
但我需要从数据库中动态填充 DataContext。我的想法是这样的:
base.DataContext = new DataTemplate[]
{
for(int i = 0; i< dsTmp.Tables[0].Rows.Count; i++)
{
new DataTemplate
{
lblText = "Count: ",
txtBoxContent = dsTmp.Tables[0].Rows[i][0].ToString();
}
}
};
当我输入这个时,它对我大喊一些语法错误;
谁能告诉我,如何正确写?
【问题讨论】:
-
如果你也写下错误会很有帮助。
-
;预期的 //// 无效的表达式术语 'for'
-
我认为,问题在于语法错误。在我看来 for 循环不能在新的 DataTemplate[]{} 中。但是我不知道怎么写。
标签: c# wpf loops datatemplate datacontext