tags:

views:

313

answers:

2

Hi, I'm pulling my hair out. I have created a class "employee.cs". I developed this class originally within the "public partial class Window1 : Window" on "Window1.xaml.cs". When moving it to a sepate class I can no longer refernce textBoxes, comboBoxes etc. What do I do?? Error given is "The name 'textBox1' does not exist in the current context". I'm sure its simple! Thanks Guys!

Here's a cut back example!

Window1.xaml

<Window x:Class="WpfApplication6.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
  <Grid>
    <TextBox Height="100" Margin="12,12,23,0" Name="textBox1" VerticalAlignment="Top" />
  </Grid>
</Window>

Window1.xaml.cs

namespace WpfApplication6
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            textBox1.Text = "testing"; //Works Here!
        }
    }
}

Class.cs

namespace WpfApplication6
{
    class class1
    {
        public static void main()
        {
            textBox1.Text = "Help"; //Doesn't Work Here!! :-(
        }
    }
}
A: 

x:Class="WpfApplication6.Window1 in the xaml Tells you this is part of the Window1 class. The window (from xaml) will be come a partial member of that class.

Preet Sangha
Thanks for your answer. Is it then possible to declare more than one class withint he xaml? Or another way for what I'm after? Thanks.
Dan Bater
no. the xmal is compiled to a clr class.
Preet Sangha
@Dan -- think of the XAML as a shorthand notation for taking regular CLR classes, instantiating them, setting their properties and putting them together. At the top level, you can define your own class that encompasses all this (via `InitialiseComponent`).
Drew Noakes
+1  A: 

As the other answer here implies, you're going to need to change your class attribute in the Window XAML.

    <Window x:Class="WpfApplication6.class1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" Height="300" Width="300">  
      <Grid>    
         <TextBox Height="100" Margin="12,12,23,0" 
                  Name="textBox1" VerticalAlignment="Top" />  
      </Grid>
    </Window>

This change should make your textbox references work.

Jacob