this.Control不起作用

|
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);
        }
    }
}
上面的代码返回以下错误: 错误1 \'WpfTestApp.MainWindow \'不包含\'Controls \'的定义,并且找不到扩展方法\'Controls \'接受类型为'WpfTestApp.MainWindow \'的第一个参数(是否缺少使用指令还是程序集引用?) tell1ѭ不提供
Controls
作为智能感知的选项。但是我之前在代码示例中看到过3。但是我似乎无法正常工作。 任何帮助将不胜感激,我在Visual Studio 2010中使用c#
已邀请:
您之前看到的代码是Windows窗体的,并且您正在尝试使用WPF。您可能要尝试:
this.FindName(myName);
这是
FrameworkElement.FindName
方法,将找到与
x:Name
相关联的东西。
Controls
是WinForms属性,但是您正在使用WPF。 WPF完全不同。例如,
Window
类不支持子级,这就是为什么没有
Controls
属性的原因。 不知道您要实现什么目标,很难推荐正确的解决方案。
采用:
this.FindName(myName);
如果您为控件分配了
x:name
,它将起作用。 请查看此答案,因为这是更可靠的解决方案。

要回复问题请先登录注册