我尝试在WPF应用程序中循环我的食物对象的名称。
我想要以下食物元素的名称:food0,food1,foot2 ...
for (int i = 0; i < foods.Count; i++)
{
System.Windows.UIElement foodelement = "food" + i; // ERROR here
Canvas.SetLeft(foodelement, foods[i].Position.X);
}
我得到了错误: Cannot implicitly convert type string to System.Windows.UIElement
这可能对您有帮助
System.Windows.UIElement foodelement = (System.Windows.UIElement)YourContainer.FindName("food" + i);
YourContainer
存放UIElement的容器(网格/画布/堆栈面板/)在哪里?
否则,您可以使用以下命令:
System.Windows.UIElement foodelement = UIHelper.FindChild<System.Windows.UIElement>(Application.Current.MainWindow, "food" + i);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句