C# 此.Control没有';行不通 使用System.Text; 使用System.Windows; 使用System.Windows.Controls; 使用System.Windows.Data; 使用System.Windows.Documents; 使用System.Windows.Input; 使用System.Windows.Media; 使用System.Windows.Media.Imaging; 使用System.Windows.Navigation; 使用System.Windows.Shapes; 使用System.Windows.Forms; 命名空间WpfTestApp { /// ///MainWindow.xaml的交互逻辑 /// 公共部分类主窗口:窗口 { 公共主窗口() { 初始化组件(); } 私有无效按钮1\u单击(对象发送者,路由目标) { 字符串myName; myName=“textBox1”; this.Controls.Find(myName); } } }

C# 此.Control没有';行不通 使用System.Text; 使用System.Windows; 使用System.Windows.Controls; 使用System.Windows.Data; 使用System.Windows.Documents; 使用System.Windows.Input; 使用System.Windows.Media; 使用System.Windows.Media.Imaging; 使用System.Windows.Navigation; 使用System.Windows.Shapes; 使用System.Windows.Forms; 命名空间WpfTestApp { /// ///MainWindow.xaml的交互逻辑 /// 公共部分类主窗口:窗口 { 公共主窗口() { 初始化组件(); } 私有无效按钮1\u单击(对象发送者,路由目标) { 字符串myName; myName=“textBox1”; this.Controls.Find(myName); } } },c#,visual-studio-2010,C#,Visual Studio 2010,上述代码返回以下错误: 错误1“WpfTestApp.MainWindow”不包含“控件”的定义,并且找不到接受“WpfTestApp.MainWindow”类型的第一个参数的扩展方法“控件”(是否缺少using指令或程序集引用?) 此。不通过intellisense提供控件作为选项。但是我以前在代码示例中见过这个.Contols.find()。但我似乎无法让它发挥作用 如果有任何帮助,我将使用c#with Visual Studio 2010控件是WinForms属性,但您使用的是WPF 这在

上述代码返回以下错误:

错误1“WpfTestApp.MainWindow”不包含“控件”的定义,并且找不到接受“WpfTestApp.MainWindow”类型的第一个参数的扩展方法“控件”(是否缺少using指令或程序集引用?)

此。
不通过intellisense提供
控件
作为选项。但是我以前在代码示例中见过
这个.Contols.find()
。但我似乎无法让它发挥作用


如果有任何帮助,我将使用c#with Visual Studio 2010

控件
是WinForms属性,但您使用的是WPF

这在WPF中是完全不同的。例如,
窗口
类不支持子类,这就是为什么没有
控件
属性的原因

如果不知道您想要实现什么目标,就很难推荐正确的解决方案。

使用:

using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;

namespace WpfTestApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string myName;
            myName = "textBox1";
            this.Controls.Find(myName);
        }
    }
}
如果您为控件指定了
x:name
,则此操作将有效


这是一个更健壮的解决方案,请查看此解决方案。

您之前看到的代码是针对的,您正在尝试使用WPF。您可能要尝试:


这是一种方法,将找到与之关联的
x:Name

ups,抱歉,错误消息您应该接受其中一个答案。建议你接受“对你最有帮助”的答案。这可以解释很多,这就是为什么我感到非常沮丧的原因。我真的没有头发了。啊@挫败Newbie-我确实觉得微软选择WinForms的
System.Windows.Forms
和WPF的
System.Windows
可能是他们犯过的最愚蠢的错误之一,包括漫画版基本上,我有20个按顺序命名的文本框(textbox1、textbox2、textbox3等),我通过一个循环运行这些文本框,比如for(int i=1;i<5;i++),它将测试一个条件是否为真,如果为真,那么我想沿着myName=“textbox”+i.ToString()的行编辑该文本框;this.Control(myName).Text=“YES”这正是我试图实现的目标。@fructednewbie try
this.FindName()
this.FindName(myName);
this.FindName(myName);