WPF的C#类,文本框和参考,简单和QUOT(?)不会在目前的情况下"存在; [英] WPF C# Classes, TextBox and Reference, Easy(?) "does not exist in the current context"
问题描述
我拉我的头发。我创建了一个类employee.cs。我最初开发中的这个类:在Window1.xaml.cs,公共部分类窗口1窗口。当它移动到sepate类我不能再refernce文本框,组合框等,我该怎么办?给定的错误是'textBox1的'这个名字并不在目前的情况下存在。我敢肯定它的简单!谢谢你们!
下面是一个缩减的例子!
的 Window1.xaml 的
<窗口x:类=WpfApplication6.Window1
的xmlns =http://schemas.microsoft。 COM / WinFX的/ 2006 / XAML /演示
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=窗口1HEIGHT =300宽=300>
<网格和GT;
<文本框高度=100保证金=12,12,23,0NAME =textBox1的VerticalAlignment =顶部/>
< /网格和GT;
< /窗GT;
的 Window1.xaml.cs 的
命名空间WpfApplication6
{
公共部分类窗口1:窗口
{
公共窗口1()
{
的InitializeComponent();
textBox1.Text =测试; //工作这里!
}
}
}
的 Class.cs 的
命名空间WpfApplication6
{
类class1
{
公共静态无效的main()
{
textBox1.Text =帮助; //不起作用在这里! :-(
}
}
}
至于其他的答案在这里意味着,你将需要改变窗口XAML类属性。
<窗口x:类=WpfApplication6.class1
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X = http://schemas.microsoft.com/winfx/2006/xaml
标题=窗口1HEIGHT =300WIDTH =300>
<网格和GT;
< ;文本框高度=100保证金=12,12,23,0
NAME =textBox1的VerticalAlignment =顶部/>
< /网格和GT;
< /窗口方式>
这变化应该让你的文本引用工作
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!! :-(
}
}
}
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.
这篇关于WPF的C#类,文本框和参考,简单和QUOT(?)不会在目前的情况下"存在;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!