3
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);
        }
    }
}

The above code returns the following error:

Error 1 'WpfTestApp.MainWindow' does not contain a definition for 'Controls' and no extension method 'Controls' accepting a first argument of type 'WpfTestApp.MainWindow' could be found (are you missing a using directive or an assembly reference?)

this. doesn't offer Controls as an option through intellisense. But I've seen this.Contols.find(<name of control>) in code examples before. But I can't seem to get it to work.

Any help would be appreciated, I'm using c# with Visual Studio 2010

Rob
  • 45,296
  • 24
  • 122
  • 150

3 Answers3

3

Controls is a WinForms property, but you are using WPF.

It's all completely different in WPF. For example, the Window class does not support children which is why there is no Controls property.

Without knowing what you are trying to achieve it's hard to recommend the right solution.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • That could explain a lot, which is why i've been getting very frustrated. I literally have no hair left. Doh! – FrustratedNewbie Apr 19 '11 at 12:46
  • 1
    @FrustratedNewbie - I do feel that Microsofts choice of `System.Windows.Forms` for WinForms and `System.Windows` for WPF was possibly **one of the stupidest mistakes they've ever made, including Comic Sans**. =) – Rob Apr 19 '11 at 12:48
  • Basically i have say 20 textboxes all named sequentially (textbox1, textbox2, textbox3 etc) and i run these through a loop, something like for (int i = 1; i < 5; i++) which will test if a condition is true or not, and if it is then i want to edit that textbox along the lines of myName = "textBox" + i.ToString(); this.Control(myName).Text = “YES” Thats roughly what i was trying to achieve. – FrustratedNewbie Apr 19 '11 at 12:55
  • @FrustratedNewbie try `this.FindName()` – David Heffernan Apr 19 '11 at 12:57
2

The code you've seen before is for Windows Forms and you're trying to work with WPF. You might want to try:

this.FindName(myName);

This is the FrameworkElement.FindName method and will find something that has an x:Name associatd with it.

Rob
  • 45,296
  • 24
  • 122
  • 150
1

Use:

this.FindName(myName);

This will work if you have assigned an x:name to your control.

Check out this answer as this is a more robust solution.

Community
  • 1
  • 1
Neil Knight
  • 47,437
  • 25
  • 129
  • 188