按名称查找控件

时间:2018-10-06 17:41:40

标签: c# wpf

我很努力地理解为什么如果设计师将控件添加到WPF表单中,那么下面的代码就可以正常工作了

TextBox MytextBox = (TextBox)MyForm.FindControl("NameTextBox");
MytextBox .Text = "You found me";

但是,只要我在运行时将控件添加到表单中,就像

Button MyButton = new Button();
MyButton.name = "YouFoundmeButton"

我将不得不使用foreach循环来测试名称。如何在不循环浏览所有控件的情况下找到该控件,而仅使用

(Button)MyForm.FindControl("YouFoundmeButton");

就像设计师在运行时创建按钮时所做的其他事情一样,我没有做。

0 个答案:

没有答案