WPF classes C #, TextBox and Reference, Easy (?) "Does not exist in the current context"

I pull my hair out. I created a class "employee.cs". I developed this class initially in the "public partial class Window1: Window" on "Window1.xaml.cs". By moving it to the sepate class, I can no longer refer to text fields, comboBoxes, etc. What should I do? The above error: "The name" textBox1 "does not exist in the current context." I am sure it is easy! Thanks guys!

Here is an example of reduction!

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!! :-(
        }
    }
}
0
source share
2 answers

As the other answer suggests, you will need to change your class attribute in the XAML window.

    <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>

.

+2

x: Class= "WpfApplication6.Window1 xaml , Window1. ( xaml) .

0

Source: https://habr.com/ru/post/1728749/


All Articles